{"record":{"id":"a07f4f678de443e5","repo":"dotnet/wpf","slug":"sr-onlysupportedoncollections","errorCode":null,"errorMessage":"SR.OnlySupportedOnCollections","messagePattern":"SR\\.OnlySupportedOnCollections","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs","lineNumber":69,"sourceCode":"\n        public EventHandler<XamlSetTypeConverterEventArgs> SetTypeConverterHandler\n        {\n            get { return _xamlType?.SetTypeConverterHandler; }\n        }\n\n        public virtual void AddToCollection(object instance, object item)\n        {\n            ArgumentNullException.ThrowIfNull(instance);\n            if (instance is IList list)\n            {\n                list.Add(item);\n                return;\n            }\n\n            ThrowIfUnknown();\n            if (!_xamlType.IsCollection)\n            {\n                throw new NotSupportedException(SR.OnlySupportedOnCollections);\n            }\n\n            XamlType itemType;\n            if (item is not null)\n            {\n                itemType = _xamlType.SchemaContext.GetXamlType(item.GetType());\n            }\n            else\n            {\n                itemType = _xamlType.ItemType;\n            }\n\n            MethodInfo addMethod = GetAddMethod(itemType);\n            if (addMethod is null)\n            {\n                throw new XamlSchemaException(SR.Format(SR.NoAddMethodFound, _xamlType, itemType));\n            }\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs#L51-L87","documentation":"XamlTypeInvoker.AddToCollection only works on XamlTypes whose IsCollection is true (a type with a usable Add method or implementing IList). Calling it on any other type throws NotSupportedException with SR.OnlySupportedOnCollections.","triggerScenarios":"Calling invoker.AddToCollection(instance, item) where the instance's XamlType is not a collection — e.g. appending to a plain POCO, a dictionary (use AddToDictionary instead), or after constructing an invoker for the wrong type.","commonSituations":"Generic XAML item-collection code that treats every object member as a collection; mismapping where a type's collection nature changed between library versions (e.g. property changed from List to single value); unit tests exercising AddToCollection on non-collection types.","solutions":["Check `_xamlType.IsCollection` (or the member's type IsCollection) before calling AddToCollection and branch accordingly.","For dictionaries use XamlTypeInvoker.AddToDictionary instead.","Fix the type mapping so the target really is a collection type, or wrap the value in a collection first."],"exampleFix":"// before\ninvoker.AddToCollection(instance, item); // NotSupportedException for non-collection\n// after\nif (typeInvoker.ShouldSerializeCollection && xamlType.IsCollection)\n    typeInvoker.AddToCollection(instance, item);\nelse if (xamlType.IsDictionary)\n    typeInvoker.AddToDictionary(instance, key, item);","handlingStrategy":"type-guard","validationCode":"bool isCollection = xamlType.IsCollection; // also consider IsDictionary","typeGuard":"static bool CanAddToCollection(XamlType t) => t.IsCollection;","tryCatchPattern":"try { invoker.AddToCollection(instance, item); }\ncatch (NotSupportedException) { /* not a collection: use AddToDictionary or skip */ }","preventionTips":["Verify XamlType.IsCollection before any AddToCollection call","Route dictionaries to AddToDictionary","Re-verify type shapes after library upgrades that may change collection members"],"tags":["wpf","system-xaml","not-supported-exception","collection"],"backgroundTag":"unsupported-operation","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"}