Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix configuration binding with types implementing IDictionary<,> #78946

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,13 @@ private static void BindConcreteDictionary(

Debug.Assert(dictionary is not null);

Type dictionaryObjectType = dictionary.GetType();
Type? iDictionaryObjectType = FindOpenGenericInterface(typeof(IDictionary<,>), dictionary.GetType());
eerhardt marked this conversation as resolved.
Show resolved Hide resolved

MethodInfo tryGetValue = dictionaryObjectType.GetMethod("TryGetValue", BindingFlags.Public | BindingFlags.Instance)!;
Debug.Assert(iDictionaryObjectType is not null);

MethodInfo tryGetValue = iDictionaryObjectType.GetMethod("TryGetValue", DeclaredOnlyLookup)!;
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
PropertyInfo? setter = iDictionaryObjectType.GetProperty("Item", DeclaredOnlyLookup);

// dictionary should be of type Dictionary<,> or of type implementing IDictionary<,>
PropertyInfo? setter = dictionaryObjectType.GetProperty("Item", BindingFlags.Public | BindingFlags.Instance);
if (setter is null || !setter.CanWrite)
{
// Cannot set any item on the dictionary object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ public void CanBindInitializedCustomIndirectlyDerivedIEnumerableList()
}

[Fact]
public void CanBindInitializedIReadOnlyDictionaryAndDoesNotMofifyTheOriginal()
public void CanBindInitializedIReadOnlyDictionaryAndDoesNotModifyTheOriginal()
{
// A field declared as IEnumerable<T> that is instantiated with a class
// that indirectly implements IEnumerable<T> is still bound, but with
Expand Down Expand Up @@ -1672,6 +1672,13 @@ public class ImplementerOfIDictionaryClass<TKey, TValue> : IDictionary<TKey, TVa
public bool TryGetValue(TKey key, out TValue value) => _dict.TryGetValue(key, out value);

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => _dict.GetEnumerator();

// The following are members which has same names as the IDictionary<,> members.
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
// Adding these to the test to ensure not getting System.Reflection.AmbiguousMatchException when we bind the dictionary.
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
private string? v;
public string? this[string key] { get => v; set => v = value; }
public bool TryGetValue() { return true; }

}

public class ExtendedDictionary<TKey, TValue> : Dictionary<TKey, TValue>
Expand Down