{"record":{"id":"1d6bc89dee7364a9","repo":"dotnet/wpf","slug":"sr-format-sr-cannotconverttype-key-gettype-int","errorCode":null,"errorMessage":"SR.Format(SR.CannotConvertType, key.GetType(), \"int\")","messagePattern":"SR\\.Format\\(SR\\.CannotConvertType, key\\.GetType\\(\\), \"int\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs","lineNumber":500,"sourceCode":"        {\n            ArgumentNullException.ThrowIfNull(key);\n\n            int value;\n\n            string s = key as string;\n            if (s != null)\n            {\n                int i = 0;\n                if (!FontFamilyMap.ParseHexNumber(s, ref i, out value) || i < s.Length)\n                    throw new ArgumentException(SR.Format(SR.CannotConvertStringToType, s, \"int\"), nameof(key));\n            }\n            else if (key is int)\n            {\n                value = (int)key;\n            }\n            else\n            {\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, key.GetType(), \"int\"), nameof(key));\n            }\n\n            if (value < 0 || value > FontFamilyMap.LastUnicodeScalar)\n                throw new ArgumentException(SR.Format(SR.CodePointOutOfRange, value), nameof(key));\n\n            return value;\n        }\n\n        private CharacterMetrics ConvertValue(object value)\n        {\n            CharacterMetrics metrics = value as CharacterMetrics;\n            if (metrics != null)\n                return metrics;\n\n            ArgumentNullException.ThrowIfNull(value);\n\n            throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(CharacterMetrics), value.GetType()));\n        }","sourceCodeStart":482,"sourceCodeEnd":518,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs#L482-L518","documentation":"CharacterMetricsDictionary accepts only int keys (or hex strings, see the string branch). ConvertKey throws this ArgumentException when the key object is neither a string nor an int — its runtime type cannot be converted to the dictionary's int key type.","triggerScenarios":"Calling Add(object, object) or the IDictionary/IDictionary<TKey,TValue> members with a boxed key of any other type (long, char, byte, double, enum) via a non-generic reference, e.g. adding through IDictionary or in XAML with an unsupported key type.","commonSituations":"Using the non-generic IDictionary interface where keys come from untyped data (deserialization, databinding); boxing a char key like 'A' expecting it to be its code point.","solutions":["Pass the key as an int Unicode scalar value (e.g. (int)'A')","If using the non-generic IDictionary, box an int, not a long/char","Cast or convert the key to int before insertion"],"exampleFix":"// before\n IDictionary dict = dictionary;\n dict.Add('A', metrics);\n// after\n dict.Add((int)'A', metrics);","handlingStrategy":"type-guard","validationCode":"bool IsValidKey(object key) => key is int i && i >= 0 && i <= 0x10FFFF;","typeGuard":"bool IsConvertibleKey(object key) => key is int || key is string;","tryCatchPattern":"try { ((IDictionary)dict).Add(key, value); } catch (ArgumentException ex) { /* key was neither int nor hex string */ }","preventionTips":["Always insert keys as int Unicode scalars","Avoid the non-generic IDictionary surface when possible; it skips compile-time key type checks","Never rely on implicit conversion of long/char keys"],"tags":["wpf","argument","type-mismatch","dictionary-key"],"backgroundTag":"type-mismatch","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"}