{"record":{"id":"eb10953ff6f558fb","repo":"dotnet/wpf","slug":"sr-onlysupportedondictionaries","errorCode":null,"errorMessage":"SR.OnlySupportedOnDictionaries","messagePattern":"SR\\.OnlySupportedOnDictionaries","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs","lineNumber":103,"sourceCode":"                throw new XamlSchemaException(SR.Format(SR.NoAddMethodFound, _xamlType, itemType));\n            }\n\n            addMethod.Invoke(instance, new object[] { item });\n        }\n\n        public virtual void AddToDictionary(object instance, object key, object item)\n        {\n            ArgumentNullException.ThrowIfNull(instance);\n            if (instance is IDictionary dictionary)\n            {\n                dictionary.Add(key, item);\n                return;\n            }\n\n            ThrowIfUnknown();\n            if (!_xamlType.IsDictionary)\n            {\n                throw new NotSupportedException(SR.OnlySupportedOnDictionaries);\n            }\n\n            XamlType itemType;\n            if (item is not null)\n            {\n                itemType = _xamlType.SchemaContext.GetXamlType(item.GetType());\n            }\n            else\n            {\n                itemType = _xamlType.ItemType;\n            }\n\n            MethodInfo addMethod = GetAddMethod(itemType);\n            if (addMethod is null)\n            {\n                throw new XamlSchemaException(SR.Format(SR.NoAddMethodFound, _xamlType, itemType));\n            }\n","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlTypeInvoker.cs#L85-L121","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check xamlType.IsDictionary before calling AddToDictionary and route collections to AddToCollection instead.","Implement IDictionary (or use Dictionary<TKey,TValue>) on the underlying type so IsDictionary is true.","Catch NotSupportedException and branch to the correct invoker method based on the XamlType shape."],"exampleFix":"// before\ninvoker.AddToDictionary(instance, key, item);\n// after\nif (xamlType.IsDictionary) { invoker.AddToDictionary(instance, key, item); } else { invoker.AddToCollection(instance, item); }","handlingStrategy":"validation","validationCode":"if (!xamlType.IsDictionary) throw new InvalidOperationException(\"AddToDictionary requires a dictionary XamlType\");","typeGuard":"static bool CanAddToDictionary(object instance, XamlType t) => instance is IDictionary || t.IsDictionary;","tryCatchPattern":"try { invoker.AddToDictionary(instance, key, item); } catch (NotSupportedException) { /* not a dictionary type */ }","preventionTips":["Route keyed items only when xamlType.IsDictionary is true; use AddToCollection otherwise.","Have dictionary-like custom types implement IDictionary.","Check the XamlType shape before choosing an invoker method.","Write schema tests covering every keyed XAML property type."],"tags":["xaml","notsupported","dictionary","api-misuse"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}