dotnet/wpf · error · NotSupportedException
SR.OnlySupportedOnCollections
Error message
SR.OnlySupportedOnCollections
What it means
XamlTypeInvoker.AddToCollection only works on XamlTypes whose IsCollection is true (a type with a usable Add method or implementing IList). Calling it on any other type throws NotSupportedException with SR.OnlySupportedOnCollections.
Solutions
- Check `_xamlType.IsCollection` (or the member's type IsCollection) before calling AddToCollection and branch accordingly.
- For dictionaries use XamlTypeInvoker.AddToDictionary instead.
- Fix the type mapping so the target really is a collection type, or wrap the value in a collection first.
Example fix
// before
invoker.AddToCollection(instance, item); // NotSupportedException for non-collection
// after
if (typeInvoker.ShouldSerializeCollection && xamlType.IsCollection)
typeInvoker.AddToCollection(instance, item);
else if (xamlType.IsDictionary)
typeInvoker.AddToDictionary(instance, key, item); Defensive patterns
Strategy: type-guard
Validate before calling
bool isCollection = xamlType.IsCollection; // also consider IsDictionary
Type guard
static bool CanAddToCollection(XamlType t) => t.IsCollection;
Try / catch
try { invoker.AddToCollection(instance, item); }
catch (NotSupportedException) { /* not a collection: use AddToDictionary or skip */ } Prevention
- Verify XamlType.IsCollection before any AddToCollection call
- Route dictionaries to AddToDictionary
- Re-verify type shapes after library upgrades that may change collection members
When it happens
Trigger: Calling invoker.AddToCollection(instance, item) where the instance's XamlType is not a collection — e.g. appending to a plain POCO, a dictionary (use AddToDictionary instead), or after constructing an invoker for the wrong type.
Common situations: Generic XAML item-collection code that treats every object member as a collection; mismapping where a type's collection nature changed between library versions (e.g. property changed from List to single value); unit tests exercising AddToCollection on non-collection types.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- SR.CantGetWriteonlyProperty
- SR.CantSetReadonlyProperty
- SR.NotSupportedOnDirective
- SR.NotSupportedOnUnknownMember
- Cannot pass multidimensional array to the CopyTo method on…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a07f4f678de443e5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs:69
public EventHandler<XamlSetTypeConverterEventArgs> SetTypeConverterHandler
{
get { return _xamlType?.SetTypeConverterHandler; }
}
public virtual void AddToCollection(object instance, object item)
{
ArgumentNullException.ThrowIfNull(instance);
if (instance is IList list)
{
list.Add(item);
return;
}
ThrowIfUnknown();
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));
}
View on GitHub (pinned to 81131a70a4)