{"record":{"id":"ed3b16686868f5e8","repo":"dotnet/wpf","slug":"sr-format-sr-ambiguousdictionaryitemtype-type","errorCode":null,"errorMessage":"SR.Format(SR.AmbiguousDictionaryItemType, type)","messagePattern":"SR\\.Format\\(SR\\.AmbiguousDictionaryItemType, type\\)","errorType":"exception","errorClass":"XamlSchemaException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/CollectionReflector.cs","lineNumber":99,"sourceCode":"\n        internal static MethodInfo LookupAddMethod(Type type, XamlCollectionKind collectionKind)\n        {\n            MethodInfo result = null;\n            switch (collectionKind)\n            {\n                case XamlCollectionKind.Collection:\n                    bool isCollection = TryGetCollectionAdder(type, mayBeICollection: true, out result);\n                    if (isCollection && result is null)\n                    {\n                        throw new XamlSchemaException(SR.Format(SR.AmbiguousCollectionItemType, type));\n                    }\n\n                    break;\n                case XamlCollectionKind.Dictionary:\n                    bool isDictionary = TryGetDictionaryAdder(type, mayBeIDictionary: true, out result);\n                    if (isDictionary && result is null)\n                    {\n                        throw new XamlSchemaException(SR.Format(SR.AmbiguousDictionaryItemType, type));\n                    }\n\n                    break;\n            }\n\n            return result;\n        }\n\n        // Returns true if the type is an ICollection<T>. Additionally, if only one <T> is\n        // implemented, returns the Add method for that type.\n        private static bool TryGetICollectionAdder(Type type, out MethodInfo addMethod)\n        {\n            bool hasMoreThanOneICollection = false;\n            Type genericICollection = GetGenericInterface(type, typeof(ICollection<>), out hasMoreThanOneICollection);\n            if (genericICollection is not null)\n            {\n                addMethod = genericICollection.GetMethod(KnownStrings.Add);\n                return true;","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/CollectionReflector.cs#L81-L117","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nclass Map : IDictionary<string, int>, IDictionary<int, string> { }\n\n// after\nclass StringIntMap : IDictionary<string, int> { }","handlingStrategy":"type-guard","validationCode":"// ensure exactly one closed IDictionary<K,V>\nvar dicts = typeof(TDict).GetInterfaces()\n    .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)).ToList();\nif (dicts.Count != 1) throw new InvalidOperationException($\"{typeof(TDict)} must implement exactly one IDictionary<TKey,TValue>\");","typeGuard":"static bool HasUniqueDictionaryPair<T>(T d) where T : IDictionary<string, object> => d.GetType().GetInterfaces().Count(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)) == 1;","tryCatchPattern":"try { result = CollectionReflector.LookupAddMethod(type, XamlCollectionKind.Dictionary); }\ncatch (XamlSchemaException ex) { log($\"Dictionary {type} has ambiguous K/V types: {ex.Message}\"); }","preventionTips":["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"],"tags":["xaml","dictionary","ambiguous-type"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}