{"record":{"id":"b67e61b6f6d20a22","repo":"unoplatform/uno","slug":"the-collection-type-0-does-not-have-add-meth","errorCode":null,"errorMessage":"The collection type '{0}' does not have 'Add' method","messagePattern":"The collection type '(.+?)' does not have 'Add' method","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeInvoker.cs","lineNumber":103,"sourceCode":"\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));\n\t\t\t\n\t\t\tmi.Invoke (instance, new object [] {item});\n\t\t}\n\n\t\tpublic virtual void AddToDictionary (object instance, object key, object item)\n\t\t{\n\t\t\tif (instance == null)\n\t\t\t\tthrow new ArgumentNullException (\"instance\");\n\n\t\t\tvar t = instance.GetType ();\n\t\t\t// FIXME: this likely needs similar method lookup to AddToCollection().\n\n\t\t\tMethodInfo mi = null;\n\t\t\tif (t.IsGenericType) {\n\t\t\t\tmi = instance.GetType ().GetMethod (\"Add\", t.GetGenericArguments ());\n\t\t\t\tif (mi == null)\n\t\t\t\t\tmi = LookupAddMethod (t, typeof (IDictionary<,>).MakeGenericType (t.GetGenericArguments ()));\n\t\t\t} else {","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeInvoker.cs#L85-L121","documentation":"Thrown by XamlTypeInvoker.AddToCollection when no `Add` method could be found on the collection instance's type via any of the tried lookup paths (direct Add, ICollection<T>, IList, Add(object)). This means the type was treated as a collection but lacks a usable Add method under reflection.","triggerScenarios":"AddToCollection on a type whose XamlType is unknown (so the IsCollection check is skipped) yet the runtime type has no Add method; or a custom collection that does not implement standard Add signatures; or trimming that removed the Add method metadata.","commonSituations":"Custom collection types with non-standard Add overloads; trimmed AOT scenarios where reflection metadata for Add was stripped; types passed as collection-shaped but actually lacking Add; version changes removing an Add method.","solutions":["Make the collection implement ICollection<T> or IList (so a standard Add is found), or expose a public `Add(T)` method.","If trimming is the cause, preserve the Add member via trimming annotations (DynamicDependency / DynamicInterfaceImplementation) or disable trimming for that type.","Ensure the correct (collection) XamlType with UnderlyingType is used so the earlier IsCollection path resolves Add properly.","Catch InvalidOperationException at the XAML read/write boundary and report which collection type failed to add."],"exampleFix":"// before\npublic class MyContainer { /* no Add method */ }\n\n// after\npublic class MyContainer : List<MyItem> { /* inherits Add(MyItem) */ }","handlingStrategy":"try-catch","validationCode":"// before AddToCollection on a non-standard type\nvar addMi = instance.GetType().GetMethod(\"Add\");\nif (addMi == null) throw new InvalidOperationException($\"{instance.GetType()} has no Add method.\");","typeGuard":null,"tryCatchPattern":"try {\n    type.Invoker.AddToCollection(instance, item);\n} catch (InvalidOperationException ex) when (ex.Message.Contains(\"'Add' method\")) {\n    // log which collection type failed, fall back to manual add or report\n}","preventionTips":["Implement ICollection<T> or IList on custom collections so a standard Add is found.","Under trimming, preserve the Add member or disable trimming for the collection type.","Ensure the correct collection XamlType (with UnderlyingType) is used so the IsCollection path resolves Add."],"tags":["xaml","schema","invoker","collection","reflection","trimming"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}