@@ -367,7 +367,7 @@ private static void BindInstance(
367
367
368
368
if ( dictionaryInterface != null )
369
369
{
370
- BindConcreteDictionary ( bindingPoint . Value ! , dictionaryInterface , config , options ) ;
370
+ BindDictionary ( bindingPoint . Value ! , dictionaryInterface , config , options ) ;
371
371
}
372
372
else
373
373
{
@@ -549,25 +549,28 @@ private static bool CanBindToTheseConstructorParameters(ParameterInfo[] construc
549
549
}
550
550
}
551
551
552
- BindConcreteDictionary ( dictionary , dictionaryType , config , options ) ;
552
+ BindDictionary ( dictionary , genericType , config , options ) ;
553
553
554
554
return dictionary ;
555
555
}
556
556
557
- // Binds and potentially overwrites a concrete dictionary.
557
+ // Binds and potentially overwrites a dictionary object .
558
558
// This differs from BindDictionaryInterface because this method doesn't clone
559
559
// the dictionary; it sets and/or overwrites values directly.
560
560
// When a user specifies a concrete dictionary or a concrete class implementing IDictionary<,>
561
561
// in their config class, then that value is used as-is. When a user specifies an interface (instantiated)
562
562
// in their config class, then it is cloned to a new dictionary, the same way as other collections.
563
563
[ RequiresDynamicCode ( DynamicCodeWarningMessage ) ]
564
564
[ RequiresUnreferencedCode ( "Cannot statically analyze what the element type is of the value objects in the dictionary so its members may be trimmed." ) ]
565
- private static void BindConcreteDictionary (
566
- object ? dictionary ,
565
+ private static void BindDictionary (
566
+ object dictionary ,
567
567
[ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicProperties | DynamicallyAccessedMemberTypes . NonPublicProperties ) ]
568
568
Type dictionaryType ,
569
569
IConfiguration config , BinderOptions options )
570
570
{
571
+ Debug . Assert ( dictionaryType . IsGenericType &&
572
+ ( dictionaryType . GetGenericTypeDefinition ( ) == typeof ( IDictionary < , > ) || dictionaryType . GetGenericTypeDefinition ( ) == typeof ( Dictionary < , > ) ) ) ;
573
+
571
574
Type keyType = dictionaryType . GenericTypeArguments [ 0 ] ;
572
575
Type valueType = dictionaryType . GenericTypeArguments [ 1 ] ;
573
576
bool keyTypeIsEnum = keyType . IsEnum ;
@@ -589,13 +592,10 @@ private static void BindConcreteDictionary(
589
592
590
593
Debug . Assert ( dictionary is not null ) ;
591
594
592
- Type dictionaryObjectType = dictionary . GetType ( ) ;
593
-
594
- MethodInfo tryGetValue = dictionaryObjectType . GetMethod ( "TryGetValue" , BindingFlags . Public | BindingFlags . Instance ) ! ;
595
+ MethodInfo tryGetValue = dictionaryType . GetMethod ( "TryGetValue" , DeclaredOnlyLookup ) ! ;
596
+ PropertyInfo ? indexerProperty = dictionaryType . GetProperty ( "Item" , DeclaredOnlyLookup ) ;
595
597
596
- // dictionary should be of type Dictionary<,> or of type implementing IDictionary<,>
597
- PropertyInfo ? setter = dictionaryObjectType . GetProperty ( "Item" , BindingFlags . Public | BindingFlags . Instance ) ;
598
- if ( setter is null || ! setter . CanWrite )
598
+ if ( indexerProperty is null || ! indexerProperty . CanWrite )
599
599
{
600
600
// Cannot set any item on the dictionary object.
601
601
return ;
@@ -623,7 +623,7 @@ private static void BindConcreteDictionary(
623
623
options : options ) ;
624
624
if ( valueBindingPoint . HasNewValue )
625
625
{
626
- setter . SetValue ( dictionary , valueBindingPoint . Value , new object [ ] { key } ) ;
626
+ indexerProperty . SetValue ( dictionary , valueBindingPoint . Value , new object [ ] { key } ) ;
627
627
}
628
628
}
629
629
catch
0 commit comments