unoplatform/uno · error · InvalidOperationException

The collection type '{0}' does not have 'Add' method

Error message

The collection type '{0}' does not have 'Add' method

What it means

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.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeInvoker.cs:103

					throw new NotSupportedException (String.Format (CultureInfo.InvariantCulture, "Non-collection type '{0}' does not support this operation", xct));
				if (ct.IsAssignableFrom (type.UnderlyingType))
					mi = GetAddMethod (type.SchemaContext.GetXamlType (item.GetType ()));
			}

			if (mi == null) {
				if (ct.IsGenericType) {
					mi = ct.GetMethod ("Add", ct.GetGenericArguments ());
					if (mi == null)
						mi = LookupAddMethod (ct, typeof (ICollection<>).MakeGenericType (ct.GetGenericArguments ()));
				} else {
					mi = ct.GetMethod ("Add", new Type [] {typeof (object)});
					if (mi == null)
						mi = LookupAddMethod (ct, typeof (IList));
				}
			}

			if (mi == null)
				throw new InvalidOperationException (String.Format (CultureInfo.InvariantCulture, "The collection type '{0}' does not have 'Add' method", ct));
			
			mi.Invoke (instance, new object [] {item});
		}

		public virtual void AddToDictionary (object instance, object key, object item)
		{
			if (instance == null)
				throw new ArgumentNullException ("instance");

			var t = instance.GetType ();
			// FIXME: this likely needs similar method lookup to AddToCollection().

			MethodInfo mi = null;
			if (t.IsGenericType) {
				mi = instance.GetType ().GetMethod ("Add", t.GetGenericArguments ());
				if (mi == null)
					mi = LookupAddMethod (t, typeof (IDictionary<,>).MakeGenericType (t.GetGenericArguments ()));
			} else {

View on GitHub (pinned to 0418340488)

Solutions

  1. Make the collection implement ICollection<T> or IList (so a standard Add is found), or expose a public `Add(T)` method.
  2. If trimming is the cause, preserve the Add member via trimming annotations (DynamicDependency / DynamicInterfaceImplementation) or disable trimming for that type.
  3. Ensure the correct (collection) XamlType with UnderlyingType is used so the earlier IsCollection path resolves Add properly.
  4. Catch InvalidOperationException at the XAML read/write boundary and report which collection type failed to add.

Example fix

// before
public class MyContainer { /* no Add method */ }

// after
public class MyContainer : List<MyItem> { /* inherits Add(MyItem) */ }
Defensive patterns

Strategy: try-catch

Validate before calling

// before AddToCollection on a non-standard type
var addMi = instance.GetType().GetMethod("Add");
if (addMi == null) throw new InvalidOperationException($"{instance.GetType()} has no Add method.");

Try / catch

try {
    type.Invoker.AddToCollection(instance, item);
} catch (InvalidOperationException ex) when (ex.Message.Contains("'Add' method")) {
    // log which collection type failed, fall back to manual add or report
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/b67e61b6f6d20a22. Report an issue: GitHub.