{"record":{"id":"814942ed5d2cb1af","repo":"dotnet/wpf","slug":"sr-format-sr-cannotconverttype-typeof-charactermetrics-value","errorCode":null,"errorMessage":"SR.Format(SR.CannotConvertType, typeof(CharacterMetrics), value.GetType())","messagePattern":"SR\\.Format\\(SR\\.CannotConvertType, typeof\\(CharacterMetrics\\), value\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs","lineNumber":517,"sourceCode":"            {\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        }\n\n        private struct Enumerator : SC.IDictionaryEnumerator, IEnumerator<KeyValuePair<int, CharacterMetrics>>\n        {\n            private CharacterMetricsDictionary _dictionary;\n            private int _unicodeScalar;\n            private CharacterMetrics _value;\n\n            internal Enumerator(CharacterMetricsDictionary dictionary)\n            {\n                _dictionary = dictionary;\n                _unicodeScalar = -1;\n                _value = null;\n            }\n\n            void IDisposable.Dispose()\n            {\n            }","sourceCodeStart":499,"sourceCodeEnd":535,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs#L499-L535","documentation":"CharacterMetricsDictionary values must be CharacterMetrics instances. ConvertValue throws this ArgumentException when the value object is of any other type (and non-null), since it cannot be converted to CharacterMetrics.","triggerScenarios":"Calling Add(object, object) or the non-generic IDictionary members with a value that is not a CharacterMetrics (e.g. a string, double, or custom glyph-metrics object) via an untyped reference.","commonSituations":"Populating the dictionary from deserialized configuration where values were strings or numbers; using the IDictionary interface with heterogeneous data.","solutions":["Pass a CharacterMetrics instance as the value","Construct one: new CharacterMetrics(...) with the advance-width/height/black-box values","Convert or map your source value to CharacterMetrics before calling Add"],"exampleFix":"// before\n dictionary.Add(65, \"0.1,0.2,0.3,0.4\");\n// after\n dictionary.Add(65, new CharacterMetrics(\"0.1,0.2,0.3,0.4\"));","handlingStrategy":"type-guard","validationCode":"bool IsValidValue(object v) => v is CharacterMetrics;","typeGuard":"bool IsCharacterMetrics(object v) => v is CharacterMetrics;","tryCatchPattern":"try { dict.Add(key, boxedValue); } catch (ArgumentException) { /* value not CharacterMetrics; construct one */ }","preventionTips":["Only insert CharacterMetrics instances","Convert string-form metrics to CharacterMetrics before insertion","Avoid heterogeneous untyped collections feeding the dictionary"],"tags":["wpf","argument","type-mismatch","dictionary-value"],"backgroundTag":"type-mismatch","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"}