{"record":{"id":"3344147ea28ecfa9","repo":"dotnet/wpf","slug":"sr-format-sr-ambiguouscollectionitemtype-type","errorCode":null,"errorMessage":"SR.Format(SR.AmbiguousCollectionItemType, type)","messagePattern":"SR\\.Format\\(SR\\.AmbiguousCollectionItemType, type\\)","errorType":"exception","errorClass":"XamlSchemaException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/CollectionReflector.cs","lineNumber":91,"sourceCode":"\n            if (TryGetCollectionAdder(type, mayBeICollection: false, out addMethod))\n            {\n                return XamlCollectionKind.Collection;\n            }\n\n            return XamlCollectionKind.None;\n        }\n\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.","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/CollectionReflector.cs#L73-L109","documentation":"CollectionReflector.LookupAddMethod throws XamlSchemaException when a type is detected as a Collection (implements ICollection<T> in a way that should yield an Add method) but TryGetCollectionAdder cannot determine a unique Add method / item type. The collection's item type is ambiguous, so XAML cannot know what element type to construct for children.","triggerScenarios":"Looking up the add method for a XamlCollectionKind.Collection type that implements multiple closed ICollection<T> interfaces (e.g. ICollection<string> and ICollection<int>) or otherwise offers no single unambiguous Add signature.","commonSituations":"Custom collection classes implementing ICollection<T> for several item types; a class changed to add a second generic collection interface after an upgrade; generic types used without a concrete closed type in XAML.","solutions":["Make the collection implement exactly one closed ICollection<T> (remove extra generic collection interfaces).","Provide an explicit public Add(T) method matching a single item type so reflection can resolve it.","Wrap the multi-interface collection in a dedicated class exposing one item type, and use that in XAML."],"exampleFix":"// before\nclass Bag : ICollection<string>, ICollection<int> { }\n\n// after\nclass StringBag : ICollection<string> { }","handlingStrategy":"type-guard","validationCode":"// ensure exactly one closed ICollection<T>\nvar i_coll = typeof(TCollection).GetInterfaces()\n    .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>)).ToList();\nif (i_coll.Count != 1) throw new InvalidOperationException($\"{typeof(TCollection)} must implement exactly one ICollection<T>\");","typeGuard":"static bool HasUniqueCollectionItem<T>(T c) where T : ICollection<string> => c.GetType().GetInterfaces().Count(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>)) == 1;","tryCatchPattern":"try { result = CollectionReflector.LookupAddMethod(type, kind); }\ncatch (XamlSchemaException ex) { log($\"Collection {type} has ambiguous item type: {ex.Message}\"); }","preventionTips":["Implement only one closed ICollection<T> per XAML-visible collection","Provide an explicit public Add(T) method","Avoid multiple generic collection interfaces on one class","Wrap multi-interface collections for XAML use"],"tags":["xaml","collection","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"}