@@ -314,7 +314,16 @@ private static void BindInstance(
314
314
return ;
315
315
}
316
316
317
- // for sets and read-only set interfaces, we clone what's there into a new collection, if we can
317
+ // -----------------------------------------------------------------------------------------------------------------------------
318
+ // | bindingPoint | bindingPoint |
319
+ // Interface | Value | IsReadOnly | Behavior
320
+ // -----------------------------------------------------------------------------------------------------------------------------
321
+ // ISet<T> | not null | true/false | Use the Value instance to populate the configuration
322
+ // ISet<T> | null | false | Create HashSet<T> instance to populate the configuration
323
+ // ISet<T> | null | true | nothing
324
+ // IReadOnlySet<T> | null/not null | false | Create HashSet<T> instance, copy over existing values, and populate the configuration
325
+ // IReadOnlySet<T> | null/not null | true | nothing
326
+ // -----------------------------------------------------------------------------------------------------------------------------
318
327
if ( TypeIsASetInterface ( type ) )
319
328
{
320
329
if ( ! bindingPoint . IsReadOnly || bindingPoint . Value is not null )
@@ -329,15 +338,25 @@ private static void BindInstance(
329
338
return ;
330
339
}
331
340
332
- // For other mutable interfaces like ICollection<>, IDictionary<,> and ISet<>, we prefer copying values and setting them
333
- // on a new instance of the interface over populating the existing instance implementing the interface.
334
- // This has already been done, so there's not need to check again.
335
- if ( TypeIsADictionaryInterface ( type ) && ! bindingPoint . IsReadOnly )
341
+ // -----------------------------------------------------------------------------------------------------------------------------
342
+ // | bindingPoint | bindingPoint |
343
+ // Interface | Value | IsReadOnly | Behavior
344
+ // -----------------------------------------------------------------------------------------------------------------------------
345
+ // IDictionary<T> | not null | true/false | Use the Value instance to populate the configuration
346
+ // IDictionary<T> | null | false | Create Dictionary<T> instance to populate the configuration
347
+ // IDictionary<T> | null | true | nothing
348
+ // IReadOnlyDictionary<T> | null/not null | false | Create Dictionary<K,V> instance, copy over existing values, and populate the configuration
349
+ // IReadOnlyDictionary<T> | null/not null | true | nothing
350
+ // -----------------------------------------------------------------------------------------------------------------------------
351
+ if ( TypeIsADictionaryInterface ( type ) )
336
352
{
337
- object ? newValue = BindDictionaryInterface ( bindingPoint . Value , type , config , options ) ;
338
- if ( newValue != null )
353
+ if ( ! bindingPoint . IsReadOnly || bindingPoint . Value is not null )
339
354
{
340
- bindingPoint . SetValue ( newValue ) ;
355
+ object ? newValue = BindDictionaryInterface ( bindingPoint . Value , type , config , options ) ;
356
+ if ( ! bindingPoint . IsReadOnly && newValue != null )
357
+ {
358
+ bindingPoint . SetValue ( newValue ) ;
359
+ }
341
360
}
342
361
343
362
return ;
@@ -538,7 +557,7 @@ private static bool CanBindToTheseConstructorParameters(ParameterInfo[] construc
538
557
if ( addMethod is null || source is null )
539
558
{
540
559
dictionaryType = typeof ( Dictionary < , > ) . MakeGenericType ( keyType , valueType ) ;
541
- var dictionary = Activator . CreateInstance ( dictionaryType ) ;
560
+ object ? dictionary = Activator . CreateInstance ( dictionaryType ) ;
542
561
addMethod = dictionaryType . GetMethod ( "Add" , DeclaredOnlyLookup ) ;
543
562
544
563
var orig = source as IEnumerable ;
@@ -748,9 +767,9 @@ private static Array BindArray(Type type, IEnumerable? source, IConfiguration co
748
767
{
749
768
Type elementType = type . GetGenericArguments ( ) [ 0 ] ;
750
769
751
- bool keyTypeIsEnum = elementType . IsEnum ;
770
+ bool elementTypeIsEnum = elementType . IsEnum ;
752
771
753
- if ( elementType != typeof ( string ) && ! keyTypeIsEnum )
772
+ if ( elementType != typeof ( string ) && ! elementTypeIsEnum )
754
773
{
755
774
// We only support string and enum keys
756
775
return null ;
0 commit comments