dotnet/wpf · error · NotSupportedException

SR.OnlySupportedOnCollectionsAndDictionaries

Error message

SR.OnlySupportedOnCollectionsAndDictionaries

What it means

XamlTypeInvoker.GetItems throws NotSupportedException('OnlySupportedOnCollectionsAndDictionaries') when the instance's XamlType is neither a collection nor a dictionary. GetItems enumerates the contents of XAML collection/dictionary types; the operation is undefined for other shapes, so it is rejected early. Instances that implement IEnumerable are handled by the fast path before this check.

Solutions

  1. Check xamlType.IsCollection || xamlType.IsDictionary before calling GetItems and skip/special-case other types.
  2. Make the instance implement IEnumerable so the fast path returns its enumerator.
  3. Catch NotSupportedException and treat the value as a single item rather than enumerable content.

Example fix

// before
var e = invoker.GetItems(value);
// after
if (value is IEnumerable e0) { var e = e0.GetEnumerator(); } else if (xamlType.IsCollection || xamlType.IsDictionary) { var e = invoker.GetItems(value); }
Defensive patterns

Strategy: type-guard

Validate before calling

bool enumerableOk = instance is IEnumerable || xamlType.IsCollection || xamlType.IsDictionary;

Type guard

static bool CanGetItems(object instance, XamlType t) => instance is IEnumerable || t.IsCollection || t.IsDictionary;

Try / catch

try { var e = invoker.GetItems(instance); } catch (NotSupportedException) { /* treat as single item */ }

Prevention

When it happens

Trigger: Calling XamlTypeInvoker.GetItems(instance) where instance is non-null, does not implement IEnumerable, the invoker is known, and _xamlType.IsCollection == false && _xamlType.IsDictionary == false — e.g. invoking it on a scalar or complex-object XamlType.

Common situations: Generic node-writer code that calls GetItems on every property value without checking the XamlType shape; schema changes where a type that used to be a collection becomes a plain object; mistakenly passing the containing object rather than the collection property value.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/28fe9913252da4f3. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs:221

            {
                return null;
            }

            return _xamlType.GetEnumeratorMethod;
        }

        public virtual IEnumerator GetItems(object instance)
        {
            ArgumentNullException.ThrowIfNull(instance);
            if (instance is IEnumerable enumerable)
            {
                return enumerable.GetEnumerator();
            }

            ThrowIfUnknown();
            if (!_xamlType.IsCollection && !_xamlType.IsDictionary)
            {
                throw new NotSupportedException(SR.OnlySupportedOnCollectionsAndDictionaries);
            }

            MethodInfo getEnumMethod = GetEnumeratorMethod();
            return (IEnumerator)getEnumMethod.Invoke(instance, Array.Empty<object>());
        }

        private bool IsPublic
        {
            get
            {
                if (_isPublic == ThreeValuedBool.NotSet)
                {
                    Type type = _xamlType.UnderlyingType.UnderlyingSystemType;
                    _isPublic = type.IsVisible ? ThreeValuedBool.True : ThreeValuedBool.False;
                }

                return _isPublic == ThreeValuedBool.True;
            }

View on GitHub (pinned to 81131a70a4)