unoplatform/uno · error · NotSupportedException
Non-collection type '{0}' does not support this operation
Error message
Non-collection type '{0}' does not support this operation What it means
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.
Source
Thrown at src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeInvoker.cs:85
public EventHandler<XamlSetTypeConverterEventArgs> SetTypeConverterHandler {
get { return type == null ? null : type.SetTypeConverterHandler; }
}
public virtual void AddToCollection (object instance, object item)
{
if (instance == null)
throw new ArgumentNullException ("instance");
if (item == null)
throw new ArgumentNullException ("item");
var ct = instance.GetType ();
var xct = type == null ? null : type.SchemaContext.GetXamlType (ct);
MethodInfo mi = null;
// FIXME: this method lookup should be mostly based on GetAddMethod(). At least iface method lookup must be done there.
if (type != null && type.UnderlyingType != null) {
if (!xct.IsCollection) // not sure why this check is done only when UnderlyingType exists...
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));View on GitHub (pinned to 0418340488)
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.
Example fix
// before
type.Invoker.AddToCollection(instance, item);
// after
if (!type.IsCollection) {
throw new InvalidOperationException($"{type} is not a collection; cannot add items.");
}
type.Invoker.AddToCollection(instance, item); Defensive patterns
Strategy: validation
Validate before calling
// before AddToCollection
if (!type.IsCollection) {
throw new InvalidOperationException($"{type} is not a collection; cannot add items.");
} Prevention
- Confirm the target type is collection-shaped (IsCollection / LookupCollectionKind) before adding.
- Verify ContentProperty mappings point at real collection properties.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- type
- Current operation is valid only when the underlying type on
- instance
- item
- The collection type '{0}' does not have 'Add' method
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/e119c7426b85e436.
Report an issue: GitHub.