{"record":{"id":"e119c7426b85e436","repo":"unoplatform/uno","slug":"non-collection-type-0-does-not-support-this-op","errorCode":null,"errorMessage":"Non-collection type '{0}' does not support this operation","messagePattern":"Non-collection type '(.+?)' does not support this operation","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeInvoker.cs","lineNumber":85,"sourceCode":"\t\tpublic EventHandler<XamlSetTypeConverterEventArgs> SetTypeConverterHandler {\n\t\t\tget { return type == null ? null : type.SetTypeConverterHandler; }\n\t\t}\n\n\t\tpublic virtual void AddToCollection (object instance, object item)\n\t\t{\n\t\t\tif (instance == null)\n\t\t\t\tthrow new ArgumentNullException (\"instance\");\n\t\t\tif (item == null)\n\t\t\t\tthrow new ArgumentNullException (\"item\");\n\n\t\t\tvar ct = instance.GetType ();\n\t\t\tvar xct = type == null ? null : type.SchemaContext.GetXamlType (ct);\n\t\t\tMethodInfo mi = null;\n\n\t\t\t// FIXME: this method lookup should be mostly based on GetAddMethod(). At least iface method lookup must be done there.\n\t\t\tif (type != null && type.UnderlyingType != null) {\n\t\t\t\tif (!xct.IsCollection) // not sure why this check is done only when UnderlyingType exists...\n\t\t\t\t\tthrow new NotSupportedException (String.Format (CultureInfo.InvariantCulture, \"Non-collection type '{0}' does not support this operation\", xct));\n\t\t\t\tif (ct.IsAssignableFrom (type.UnderlyingType))\n\t\t\t\t\tmi = GetAddMethod (type.SchemaContext.GetXamlType (item.GetType ()));\n\t\t\t}\n\n\t\t\tif (mi == null) {\n\t\t\t\tif (ct.IsGenericType) {\n\t\t\t\t\tmi = ct.GetMethod (\"Add\", ct.GetGenericArguments ());\n\t\t\t\t\tif (mi == null)\n\t\t\t\t\t\tmi = LookupAddMethod (ct, typeof (ICollection<>).MakeGenericType (ct.GetGenericArguments ()));\n\t\t\t\t} else {\n\t\t\t\t\tmi = ct.GetMethod (\"Add\", new Type [] {typeof (object)});\n\t\t\t\t\tif (mi == null)\n\t\t\t\t\t\tmi = LookupAddMethod (ct, typeof (IList));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (mi == null)\n\t\t\t\tthrow new InvalidOperationException (String.Format (CultureInfo.InvariantCulture, \"The collection type '{0}' does not have 'Add' method\", ct));","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeInvoker.cs#L67-L103","documentation":"Thrown by XamlTypeInvoker.AddToCollection when the instance's XamlType is known (UnderlyingType non-null) but IsCollection is false. The operation only makes sense on collections; a non-collection type has no Add semantics. This early check rejects the operation before attempting reflection.","triggerScenarios":"Calling AddToCollection on an instance whose XamlType is not flagged as a collection — e.g. a scalar object, a class without IEnumerable/IList/Add, or a type whose CollectionKind resolved to None.","commonSituations":"XAML where an element is treated as a content collection but the target type is not a collection (mismatched ContentProperty, wrong type mapping); custom writers that assume a property holds a collection when it holds a single object; type-resolution returning a different (non-collection) XamlType than expected.","solutions":["Verify the target type implements IEnumerable/IList/Add before calling AddToCollection — check xamlType.IsCollection / LookupCollectionKind().","Correct the XAML: ensure the property/element is actually a collection type, or fix the ContentProperty attribute.","Fix the XamlSchemaContext type mapping so the right (collection) XamlType is resolved."],"exampleFix":"// before\ntype.Invoker.AddToCollection(instance, item);\n\n// after\nif (!type.IsCollection) {\n    throw new InvalidOperationException($\"{type} is not a collection; cannot add items.\");\n}\ntype.Invoker.AddToCollection(instance, item);","handlingStrategy":"validation","validationCode":"// before AddToCollection\nif (!type.IsCollection) {\n    throw new InvalidOperationException($\"{type} is not a collection; cannot add items.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Confirm the target type is collection-shaped (IsCollection / LookupCollectionKind) before adding.","Verify ContentProperty mappings point at real collection properties."],"tags":["xaml","schema","invoker","collection","type-resolution"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}