dotnet/wpf · error · XamlSchemaException
SR.NoAddMethodFound
Error message
SR.NoAddMethodFound
What it means
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.
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.
Example fix
// before
class MyCollection : IEnumerable<Item> { internal void Add(Item i) { ... } }
// after
class MyCollection : IEnumerable<Item> { public void Add(Item i) { ... } } Defensive patterns
Strategy: validation
Validate before calling
bool canAdd = instance is IList || (xamlType.IsCollection && xamlType.UnderlyingType?.GetMethod("Add", new[]{ xamlType.ItemType?.UnderlyingType ?? typeof(object) }) != null); Type guard
static bool HasPublicAdd(Type t, Type itemType) => t.GetMethod("Add", new[] { itemType })?.IsPublic == true; Try / catch
try { invoker.AddToCollection(instance, item); } catch (XamlSchemaException ex) { /* no Add method on collection */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.
Related errors
- ' '.' ' is a property without a getter and is not a valid…
- ' '.' ' is a property without a getter and is not a valid…
- ' '.' ' is a property without a getter and is not a valid…
- SR.ConverterMustDeriveFromBase
- SR.Format(SR.AmbiguousCollectionItemType, type)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/7b694c1b494c1140.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs:85
if (!_xamlType.IsCollection)
{
throw new NotSupportedException(SR.OnlySupportedOnCollections);
}
XamlType itemType;
if (item is not null)
{
itemType = _xamlType.SchemaContext.GetXamlType(item.GetType());
}
else
{
itemType = _xamlType.ItemType;
}
MethodInfo addMethod = GetAddMethod(itemType);
if (addMethod is null)
{
throw new XamlSchemaException(SR.Format(SR.NoAddMethodFound, _xamlType, itemType));
}
addMethod.Invoke(instance, new object[] { item });
}
public virtual void AddToDictionary(object instance, object key, object item)
{
ArgumentNullException.ThrowIfNull(instance);
if (instance is IDictionary dictionary)
{
dictionary.Add(key, item);
return;
}
ThrowIfUnknown();
if (!_xamlType.IsDictionary)
{
throw new NotSupportedException(SR.OnlySupportedOnDictionaries);View on GitHub (pinned to 81131a70a4)