@@ -225,8 +225,6 @@ public object CreateInstance(Type type, bool enablePrivate)
225
225
nameof ( type ) ) ;
226
226
}
227
227
228
- CacheInstances ( arguments . Zip ( ctor . GetParameters ( ) , ( i , p ) => ( p . ParameterType , i ) ) ) ;
229
-
230
228
try
231
229
{
232
230
object ? [ ] parameters = arguments . Select ( x => x . Value ) . ToArray ( ) ;
@@ -958,7 +956,6 @@ public void Verify<T, TResult>(Expression<Func<T, TResult>> expression, Times ti
958
956
TryGetConstructorInvocation ( serviceType , objectGraphContext , out ConstructorInfo ? ctor , out IInstance [ ] ? arguments ) )
959
957
{
960
958
constructorArgs = arguments . Select ( x => x . Value ) . ToArray ( ) ;
961
- CacheInstances ( arguments . Zip ( ctor . GetParameters ( ) , ( i , p ) => ( p . ParameterType , i ) ) ) ;
962
959
}
963
960
964
961
if ( Activator . CreateInstance ( mockType , mockBehavior , constructorArgs ) is Mock mock )
@@ -1018,43 +1015,37 @@ bool TryCreateArguments(ConstructorInfo constructor, ObjectGraphContext context,
1018
1015
{
1019
1016
return false ;
1020
1017
}
1018
+
1019
+ TryCache ( parameters [ i ] . ParameterType , service ) ;
1021
1020
arguments [ i ] = service ;
1022
1021
}
1023
1022
return true ;
1024
1023
}
1024
+
1025
+ }
1026
+
1027
+ private void TryCache ( Type type , IInstance instance )
1028
+ {
1029
+ WithTypeMap ( typeMap =>
1030
+ {
1031
+ if ( ! typeMap . TryGetValue ( type , out _ ) )
1032
+ {
1033
+ typeMap [ type ] = instance ;
1034
+ }
1035
+ } ) ;
1025
1036
}
1026
1037
1027
1038
private Mock GetOrMakeMockFor ( Type type )
1028
1039
{
1029
1040
if ( TryResolve ( type , new ObjectGraphContext ( false ) , out IInstance ? instance ) &&
1030
1041
instance is MockInstance mockInstance )
1031
1042
{
1032
- WithTypeMap ( typeMap =>
1033
- {
1034
- if ( ! typeMap . ContainsKey ( type ) )
1035
- {
1036
- typeMap [ type ] = mockInstance ;
1037
- }
1038
- } ) ;
1043
+ TryCache ( type , mockInstance ) ;
1039
1044
return mockInstance . Mock ;
1040
1045
}
1041
1046
throw new ArgumentException ( $ "{ type } does not resolve to a Mock") ;
1042
1047
}
1043
1048
1044
- internal void CacheInstances ( IEnumerable < ( Type , IInstance ) > instances )
1045
- {
1046
- WithTypeMap ( typeMap =>
1047
- {
1048
- foreach ( var ( type , instance ) in instances )
1049
- {
1050
- if ( ! typeMap . ContainsKey ( type ) )
1051
- {
1052
- typeMap [ type ] = instance ;
1053
- }
1054
- }
1055
- } ) ;
1056
- }
1057
-
1058
1049
private void WithTypeMap ( Action < NonBlocking . ConcurrentDictionary < Type , IInstance > > onTypeMap )
1059
1050
{
1060
1051
if ( TypeMap is { } typeMap )
0 commit comments