dotnet/wpf · error · NotSupportedException

SR.OnlySupportedOnDictionaries

Error message

SR.OnlySupportedOnDictionaries

What it means

XamlTypeInvoker.AddToDictionary throws NotSupportedException('OnlySupportedOnDictionaries') when the instance's XamlType is not marked IsDictionary. The invoker can only add keyed items to dictionary-shaped types; anything else is rejected before looking up an Add(key, item) method. Unlike the XamlSchemaException cases, this is a plain NotSupportedException indicating the caller used the wrong API for the type.

Solutions

  1. Check xamlType.IsDictionary before calling AddToDictionary and route collections to AddToCollection instead.
  2. Implement IDictionary (or use Dictionary<TKey,TValue>) on the underlying type so IsDictionary is true.
  3. Catch NotSupportedException and branch to the correct invoker method based on the XamlType shape.

Example fix

// before
invoker.AddToDictionary(instance, key, item);
// after
if (xamlType.IsDictionary) { invoker.AddToDictionary(instance, key, item); } else { invoker.AddToCollection(instance, item); }
Defensive patterns

Strategy: validation

Validate before calling

if (!xamlType.IsDictionary) throw new InvalidOperationException("AddToDictionary requires a dictionary XamlType");

Type guard

static bool CanAddToDictionary(object instance, XamlType t) => instance is IDictionary || t.IsDictionary;

Try / catch

try { invoker.AddToDictionary(instance, key, item); } catch (NotSupportedException) { /* not a dictionary type */ }

Prevention

When it happens

Trigger: Calling XamlTypeInvoker.AddToDictionary(instance, key, item) where instance is non-null, not an IDictionary, the invoker is known (not the Unknown invoker), and _xamlType.IsDictionary is false — i.e. invoking dictionary semantics on a plain collection or scalar-backed XamlType.

Common situations: Authoring a custom XAML writer/parser that routes x:Key items through AddToDictionary without first checking XamlType.IsDictionary; misclassifying a key-value custom type as a dictionary in schema code; copy-pasting AddToDictionary where AddToCollection was intended.

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/eb10953ff6f558fb. Report an issue: GitHub.

Appendix: source

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

                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);
            }

            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)