Skip to content
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
Next Next commit
Fix configuration binding with types implementing IDictionary
  • Loading branch information
tarekgh committed Nov 29, 2022
commit 96ef7439d388b69a4d3f723dbce74fc5bbc916b4
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());

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

MethodInfo tryGetValue = iDictionaryObjectType.GetMethod("TryGetValue", DeclaredOnlyLookup)!;
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.
// Adding these to the test to ensure not getting System.Reflection.AmbiguousMatchException when we bind the dictionary.
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