Skip to content

Commit f0aedc7

Browse files
authored
Small ConfigurationBinder Cleanup (#79042)
Now that we know dictionaryType is either the built-in IDictionary or Dictionary types, we don't need to check if the indexer property is missing or if it is not writeable. Removing unnecessary checks. Also removing a few nullable suppressions and adding a Debug.Assert instead.
1 parent 3559d33 commit f0aedc7

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -361,24 +361,26 @@ private static void BindInstance(
361361
}
362362
}
363363

364+
Debug.Assert(bindingPoint.Value is not null);
365+
364366
// At this point we know that we have a non-null bindingPoint.Value, we just have to populate the items
365367
// using the IDictionary<> or ICollection<> interfaces, or properties using reflection.
366368
Type? dictionaryInterface = FindOpenGenericInterface(typeof(IDictionary<,>), type);
367369

368370
if (dictionaryInterface != null)
369371
{
370-
BindDictionary(bindingPoint.Value!, dictionaryInterface, config, options);
372+
BindDictionary(bindingPoint.Value, dictionaryInterface, config, options);
371373
}
372374
else
373375
{
374376
Type? collectionInterface = FindOpenGenericInterface(typeof(ICollection<>), type);
375377
if (collectionInterface != null)
376378
{
377-
BindCollection(bindingPoint.Value!, collectionInterface, config, options);
379+
BindCollection(bindingPoint.Value, collectionInterface, config, options);
378380
}
379381
else
380382
{
381-
BindProperties(bindingPoint.Value!, config, options);
383+
BindProperties(bindingPoint.Value, config, options);
382384
}
383385
}
384386
}
@@ -590,17 +592,8 @@ private static void BindDictionary(
590592
return;
591593
}
592594

593-
Debug.Assert(dictionary is not null);
594-
595-
596595
MethodInfo tryGetValue = dictionaryType.GetMethod("TryGetValue", DeclaredOnlyLookup)!;
597-
PropertyInfo? indexerProperty = dictionaryType.GetProperty("Item", DeclaredOnlyLookup);
598-
599-
if (indexerProperty is null || !indexerProperty.CanWrite)
600-
{
601-
// Cannot set any item on the dictionary object.
602-
return;
603-
}
596+
PropertyInfo indexerProperty = dictionaryType.GetProperty("Item", DeclaredOnlyLookup)!;
604597

605598
foreach (IConfigurationSection child in config.GetChildren())
606599
{

0 commit comments

Comments
 (0)