dotnet/wpf · error · XamlSchemaException
SR.Format(SR.AmbiguousDictionaryItemType, type)
Error message
SR.Format(SR.AmbiguousDictionaryItemType, type)
What it means
CollectionReflector.LookupAddMethod throws XamlSchemaException when a type is detected as a Dictionary (XamlCollectionKind.Dictionary) but TryGetDictionaryAdder cannot resolve a unique key/value item type. The dictionary's K,V pair is ambiguous, so XAML cannot construct entries for it.
Solutions
- Implement exactly one closed IDictionary<TKey,TValue> on the dictionary type.
- Ensure a single public Add(TKey, TValue) method exists so reflection resolves one signature.
- Use a wrapper dictionary class with one explicit K,V pair in XAML.
Example fix
// before
class Map : IDictionary<string, int>, IDictionary<int, string> { }
// after
class StringIntMap : IDictionary<string, int> { } Defensive patterns
Strategy: type-guard
Validate before calling
// ensure exactly one closed IDictionary<K,V>
var dicts = typeof(TDict).GetInterfaces()
.Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)).ToList();
if (dicts.Count != 1) throw new InvalidOperationException($"{typeof(TDict)} must implement exactly one IDictionary<TKey,TValue>"); Type guard
static bool HasUniqueDictionaryPair<T>(T d) where T : IDictionary<string, object> => d.GetType().GetInterfaces().Count(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)) == 1;
Try / catch
try { result = CollectionReflector.LookupAddMethod(type, XamlCollectionKind.Dictionary); }
catch (XamlSchemaException ex) { log($"Dictionary {type} has ambiguous K/V types: {ex.Message}"); } Prevention
- Implement one closed IDictionary<TKey,TValue> per XAML dictionary
- Keep a single public Add(TKey, TValue) overload
- Do not mix non-generic IDictionary with generic IDictionary on the same type
- Prefer Dictionary<K,V> directly in XAML object models
When it happens
Trigger: Resolving the add method for a XamlCollectionKind.Dictionary type that implements IDictionary with multiple incompatible key/value signatures (e.g. IDictionary<string,object> plus another IDictionary<K,V>) so no single unambiguous Add(key, value) exists.
Common situations: Custom dictionary classes implementing several closed IDictionary<K,V> interfaces; refactoring that added a second dictionary interface; non-generic IDictionary combined with generic IDictionary in one type.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- ArgumentException: path
- ArgumentException: relativeTo
- ArgumentNullException: path
- ArgumentNullException: relativeTo
- InitializationState !=
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ed3b16686868f5e8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/CollectionReflector.cs:99
internal static MethodInfo LookupAddMethod(Type type, XamlCollectionKind collectionKind)
{
MethodInfo result = null;
switch (collectionKind)
{
case XamlCollectionKind.Collection:
bool isCollection = TryGetCollectionAdder(type, mayBeICollection: true, out result);
if (isCollection && result is null)
{
throw new XamlSchemaException(SR.Format(SR.AmbiguousCollectionItemType, type));
}
break;
case XamlCollectionKind.Dictionary:
bool isDictionary = TryGetDictionaryAdder(type, mayBeIDictionary: true, out result);
if (isDictionary && result is null)
{
throw new XamlSchemaException(SR.Format(SR.AmbiguousDictionaryItemType, type));
}
break;
}
return result;
}
// Returns true if the type is an ICollection<T>. Additionally, if only one <T> is
// implemented, returns the Add method for that type.
private static bool TryGetICollectionAdder(Type type, out MethodInfo addMethod)
{
bool hasMoreThanOneICollection = false;
Type genericICollection = GetGenericInterface(type, typeof(ICollection<>), out hasMoreThanOneICollection);
if (genericICollection is not null)
{
addMethod = genericICollection.GetMethod(KnownStrings.Add);
return true;View on GitHub (pinned to 81131a70a4)