Skip to content

Commit

Permalink
Small ConfigurationBinder Cleanup (#79042)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eerhardt authored Dec 5, 2022
1 parent 3559d33 commit f0aedc7
Showing 1 changed file with 6 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,24 +361,26 @@ private static void BindInstance(
}
}

Debug.Assert(bindingPoint.Value is not null);

// At this point we know that we have a non-null bindingPoint.Value, we just have to populate the items
// using the IDictionary<> or ICollection<> interfaces, or properties using reflection.
Type? dictionaryInterface = FindOpenGenericInterface(typeof(IDictionary<,>), type);

if (dictionaryInterface != null)
{
BindDictionary(bindingPoint.Value!, dictionaryInterface, config, options);
BindDictionary(bindingPoint.Value, dictionaryInterface, config, options);
}
else
{
Type? collectionInterface = FindOpenGenericInterface(typeof(ICollection<>), type);
if (collectionInterface != null)
{
BindCollection(bindingPoint.Value!, collectionInterface, config, options);
BindCollection(bindingPoint.Value, collectionInterface, config, options);
}
else
{
BindProperties(bindingPoint.Value!, config, options);
BindProperties(bindingPoint.Value, config, options);
}
}
}
Expand Down Expand Up @@ -590,17 +592,8 @@ private static void BindDictionary(
return;
}

Debug.Assert(dictionary is not null);


MethodInfo tryGetValue = dictionaryType.GetMethod("TryGetValue", DeclaredOnlyLookup)!;
PropertyInfo? indexerProperty = dictionaryType.GetProperty("Item", DeclaredOnlyLookup);

if (indexerProperty is null || !indexerProperty.CanWrite)
{
// Cannot set any item on the dictionary object.
return;
}
PropertyInfo indexerProperty = dictionaryType.GetProperty("Item", DeclaredOnlyLookup)!;

foreach (IConfigurationSection child in config.GetChildren())
{
Expand Down

0 comments on commit f0aedc7

Please sign in to comment.