{"record":{"id":"7b694c1b494c1140","repo":"dotnet/wpf","slug":"sr-noaddmethodfound","errorCode":null,"errorMessage":"SR.NoAddMethodFound","messagePattern":"SR\\.NoAddMethodFound","errorType":"exception","errorClass":"XamlSchemaException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs","lineNumber":85,"sourceCode":"            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\n            addMethod.Invoke(instance, new object[] { item });\n        }\n\n        public virtual void AddToDictionary(object instance, object key, object item)\n        {\n            ArgumentNullException.ThrowIfNull(instance);\n            if (instance is IDictionary dictionary)\n            {\n                dictionary.Add(key, item);\n                return;\n            }\n\n            ThrowIfUnknown();\n            if (!_xamlType.IsDictionary)\n            {\n                throw new NotSupportedException(SR.OnlySupportedOnDictionaries);","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs#L67-L103","documentation":"XamlTypeInvoker.AddToCollection throws XamlSchemaException('NoAddMethodFound') when the target XamlType is a known collection type, but reflection cannot find a public Add method on the underlying CLR type that accepts an item of the required item type. System.Xaml collects collection items during XAML node-stream processing by invoking Add; without a usable Add method it cannot materialize the collection, so it fails fast with a schema exception naming the XamlType and item type.","triggerScenarios":"Calling XamlTypeInvoker.AddToCollection(instance, item) on an instance whose XamlType.IsCollection is true but whose underlying type exposes no public Add(itemType) method — e.g. a custom collection class that only has an internal Add, or an Add whose parameter type does not match XamlType.ItemType. The IList fast path (instance is IList) bypasses this, so the error only fires for collections that are not IList.","commonSituations":"Custom collection types used as XAML content properties where Add is private/internal or renamed; a XamlType.ItemType inferred from a generic Add(ICollection<T>) signature that no concrete Add overload matches; assemblies trimmed or refactored so the Add method is no longer public at load time.","solutions":["Make the collection's Add method public and give it a single parameter assignable from the XamlType.ItemType.","If the type is not truly a collection, correct the XAML/schema so XamlType.IsCollection is false, or have the instance implement IList so the fast path is used.","Catch XamlSchemaException around AddToCollection and fall back to custom item-collection logic.","Use a XamlTypeInvoker subclass overriding AddToCollection to supply custom add behavior."],"exampleFix":"// before\nclass MyCollection : IEnumerable<Item> { internal void Add(Item i) { ... } }\n// after\nclass MyCollection : IEnumerable<Item> { public void Add(Item i) { ... } }","handlingStrategy":"validation","validationCode":"bool canAdd = instance is IList || (xamlType.IsCollection && xamlType.UnderlyingType?.GetMethod(\"Add\", new[]{ xamlType.ItemType?.UnderlyingType ?? typeof(object) }) != null);","typeGuard":"static bool HasPublicAdd(Type t, Type itemType) => t.GetMethod(\"Add\", new[] { itemType })?.IsPublic == true;","tryCatchPattern":"try { invoker.AddToCollection(instance, item); } catch (XamlSchemaException ex) { /* no Add method on collection */ }","preventionTips":["Make collection Add methods public with a parameter matching the content/item type.","Prefer implementing IList<T> or IList so the invoker's fast path applies.","Check xamlType.IsCollection and the Add method via reflection before programmatic item insertion.","Add unit tests that round-trip collections through XamlTypeInvoker."],"tags":["xaml","reflection","schema-exception","collection"],"backgroundTag":"missing-dependency","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"}