@@ -79,15 +79,16 @@ bool TryCreateContainer([NotNullWhen(true)] out object? result)
79
79
{
80
80
result = null ;
81
81
82
- if ( IsConstructableDictionary ( toType , elementType , out var concreteType , out var addMethod ) )
82
+ if ( IsConstructableDictionary ( toType , elementType , out var concreteType , out var keyType , out var valueType , out var addMethod ) )
83
83
{
84
84
result = Activator . CreateInstance ( concreteType ) ?? throw new InvalidOperationException ( $ "Activator.CreateInstance returned null for { concreteType } ") ;
85
85
86
86
foreach ( var section in _section . GetChildren ( ) )
87
87
{
88
88
var argumentValue = ConfigurationReader . GetArgumentValue ( section , _configurationAssemblies ) ;
89
- var value = argumentValue . ConvertTo ( elementType , resolutionContext ) ;
90
- addMethod . Invoke ( result , new [ ] { section . Key , value } ) ;
89
+ var key = new StringArgumentValue ( section . Key ) . ConvertTo ( keyType , resolutionContext ) ;
90
+ var value = argumentValue . ConvertTo ( valueType , resolutionContext ) ;
91
+ addMethod . Invoke ( result , new [ ] { key , value } ) ;
91
92
}
92
93
return true ;
93
94
}
@@ -262,21 +263,23 @@ static bool TryBindToCtorArgument(object value, Type type, ResolutionContext res
262
263
{
263
264
argumentExpression = Expression . Convert ( ctorExpression , type ) ;
264
265
}
265
- else {
266
+ else
267
+ {
266
268
argumentExpression = ctorExpression ;
267
269
}
268
270
return true ;
269
271
}
270
272
if ( IsContainer ( type , out elementType ) )
271
273
{
272
- if ( IsConstructableDictionary ( type , elementType , out var concreteType , out var addMethod ) )
274
+ if ( IsConstructableDictionary ( type , elementType , out var concreteType , out var keyType , out var valueType , out var addMethod ) )
273
275
{
274
276
var elements = new List < ElementInit > ( ) ;
275
277
foreach ( var element in s . GetChildren ( ) )
276
278
{
277
- if ( TryBindToCtorArgument ( element , elementType , resolutionContext , out var elementExpression ) )
279
+ if ( TryBindToCtorArgument ( element , valueType , resolutionContext , out var elementExpression ) )
278
280
{
279
- elements . Add ( Expression . ElementInit ( addMethod , Expression . Constant ( element . Key ) , elementExpression ) ) ;
281
+ var key = new StringArgumentValue ( element . Key ) . ConvertTo ( keyType , resolutionContext ) ;
282
+ elements . Add ( Expression . ElementInit ( addMethod , Expression . Constant ( key , keyType ) , elementExpression ) ) ;
280
283
}
281
284
else
282
285
{
@@ -337,24 +340,19 @@ static bool IsContainer(Type type, [NotNullWhen(true)] out Type? elementType)
337
340
return false ;
338
341
}
339
342
340
- static bool IsConstructableDictionary ( Type type , Type elementType , [ NotNullWhen ( true ) ] out Type ? concreteType , [ NotNullWhen ( true ) ] out MethodInfo ? addMethod )
343
+ static bool IsConstructableDictionary ( Type type , Type elementType , [ NotNullWhen ( true ) ] out Type ? concreteType , [ NotNullWhen ( true ) ] out Type ? keyType , [ NotNullWhen ( true ) ] out Type ? valueType , [ NotNullWhen ( true ) ] out MethodInfo ? addMethod )
341
344
{
342
345
concreteType = null ;
346
+ keyType = null ;
347
+ valueType = null ;
343
348
addMethod = null ;
344
349
if ( ! elementType . IsGenericType || elementType . GetGenericTypeDefinition ( ) != typeof ( KeyValuePair < , > ) )
345
350
{
346
351
return false ;
347
352
}
348
353
var argumentTypes = elementType . GetGenericArguments ( ) ;
349
- if ( argumentTypes [ 0 ] != typeof ( string ) )
350
- {
351
- return false ;
352
- }
353
- if ( ! typeof ( IDictionary < , > ) . MakeGenericType ( argumentTypes ) . IsAssignableFrom ( type )
354
- && ! typeof ( IReadOnlyDictionary < , > ) . MakeGenericType ( argumentTypes ) . IsAssignableFrom ( type ) )
355
- {
356
- return false ;
357
- }
354
+ keyType = argumentTypes [ 0 ] ;
355
+ valueType = argumentTypes [ 1 ] ;
358
356
if ( type . IsAbstract )
359
357
{
360
358
concreteType = typeof ( Dictionary < , > ) . MakeGenericType ( argumentTypes ) ;
@@ -371,13 +369,12 @@ static bool IsConstructableDictionary(Type type, Type elementType, [NotNullWhen(
371
369
{
372
370
return false ;
373
371
}
374
- var valueType = argumentTypes [ 1 ] ;
375
372
foreach ( var method in concreteType . GetMethods ( ) )
376
373
{
377
374
if ( ! method . IsStatic && method . Name == "Add" )
378
375
{
379
376
var parameters = method . GetParameters ( ) ;
380
- if ( parameters . Length == 2 && parameters [ 0 ] . ParameterType == typeof ( string ) && parameters [ 1 ] . ParameterType == valueType )
377
+ if ( parameters . Length == 2 && parameters [ 0 ] . ParameterType == keyType && parameters [ 1 ] . ParameterType == valueType )
381
378
{
382
379
addMethod = method ;
383
380
return true ;
0 commit comments