{"record":{"id":"5b9f24e4219eb7ab","repo":"dotnet/wpf","slug":"sr-format-sr-mustbeoftype-value-systemresourcekey-or","errorCode":null,"errorMessage":"SR.Format(SR.MustBeOfType, \"value\", \"SystemResourceKey or SystemThemeKey\")","messagePattern":"SR\\.Format\\(SR\\.MustBeOfType, \"value\", \"SystemResourceKey or SystemThemeKey\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemKeyConverter.cs","lineNumber":132,"sourceCode":"            if (destinationType == typeof(MarkupExtension)\n                &&\n                CanConvertTo(context, destinationType) )\n            {\n                SystemResourceKeyID keyId;\n\n                // Get the SystemResourceKeyID\n                \n                if( value is SystemResourceKey )\n                {\n                    keyId = (value as SystemResourceKey).InternalKey;\n                }\n                else if( value is SystemThemeKey )\n                {\n                    keyId = (value as SystemThemeKey).InternalKey;\n                }\n                else\n                {\n                    throw new ArgumentException(SR.Format(SR.MustBeOfType, \"value\", \"SystemResourceKey or SystemThemeKey\")); \n                }\n\n                // System resource keys can be converted into a MarkupExtension (StaticExtension)\n                \n                Type keyType = SystemKeyConverter.GetSystemClassType(keyId);\n\n                // Get the value serialization context\n                IValueSerializerContext valueSerializerContext = context as IValueSerializerContext;\n                if( valueSerializerContext != null )\n                {\n                    // And from that get a System.Type serializer\n                    ValueSerializer typeSerializer = valueSerializerContext.GetValueSerializerFor(typeof(Type));\n                    if( typeSerializer != null )\n                    {\n                        // And use that to create the string-ized class name\n                        string systemClassName = typeSerializer.ConvertToString(keyType, valueSerializerContext);\n\n                        // And finally create the StaticExtension.","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemKeyConverter.cs#L114-L150","documentation":"SystemKeyConverter.ConvertTo only accepts SystemResourceKey or SystemThemeKey as the value being converted; any other object type makes it throw ArgumentException. The converter exists so WPF system resource keys (e.g. SystemParameters.CaptionHeightKey) can round-trip to a StaticExtension markup string for XAML serialization. Passing an unconvertible value is a programming error, so the exception names the two accepted types.","triggerScenarios":"Calling TypeConverter.ConvertTo (or TypeDescriptor.GetConverter(new SystemResourceKey(...)).ConvertTo(null, culture, value, destinationType)) with a value that is neither SystemResourceKey nor SystemThemeKey, e.g. passing a raw enum, a string, or a different key type.","commonSituations":"Custom XAML serialization/markup extension code that walks resource dictionaries and assumes every value is a system key; converters reused with arbitrary keys like DynamicResourceExtension values; refactoring that changed the key type without updating the converter call.","solutions":["Check the value's type before calling ConvertTo and only pass SystemResourceKey or SystemThemeKey instances.","If converting other key kinds, dispatch to the correct converter (e.g. the TypeConverter for that key's type) instead of SystemKeyConverter.","Wrap the ConvertTo call in try/catch(ArgumentException) when the input is untrusted and skip/convert via ToString() instead."],"exampleFix":"// before\nstring s = (string)converter.ConvertTo(null, CultureInfo.InvariantCulture, value, typeof(MarkupExtension));\n// after\nif (value is SystemResourceKey || value is SystemThemeKey)\n{\n    string s = (string)converter.ConvertTo(null, CultureInfo.InvariantCulture, value, typeof(MarkupExtension));\n}\nelse\n{\n    string s = value?.ToString() ?? string.Empty; // or skip non-system keys\n}","handlingStrategy":"type-guard","validationCode":"if (value is SystemResourceKey || value is SystemThemeKey)\n{\n    var ext = (MarkupExtension)converter.ConvertTo(null, CultureInfo.InvariantCulture, value, typeof(MarkupExtension));\n}\nelse\n{\n    throw new ArgumentException($\"Cannot convert {value?.GetType().Name}; expected SystemResourceKey or SystemThemeKey\");\n}","typeGuard":"bool IsSystemKey(object value) => value is SystemResourceKey || value is SystemThemeKey;","tryCatchPattern":"try\n{\n    var ext = (MarkupExtension)converter.ConvertTo(null, culture, value, typeof(MarkupExtension));\n}\ncatch (ArgumentException ex)\n{\n    // value was not a system key; log and handle non-convertible input\n}","preventionTips":["Only feed SystemResourceKey/SystemThemeKey instances into SystemKeyConverter","Route other key types to their own TypeConverter","Unit-test converters with representative value types","Add a type assert/guard before ConvertTo in serialization helpers"],"tags":["wpf","type-converter","argumentexception"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}