{"record":{"id":"745bf511a255fe94","repo":"dotnet/wpf","slug":"sr-format-sr-cannotconverttype-obj-gettype-typeof","errorCode":null,"errorMessage":"SR.Format(SR.CannotConvertType, obj.GetType(), typeof(FontFamilyMap))","messagePattern":"SR\\.Format\\(SR\\.CannotConvertType, obj\\.GetType\\(\\), typeof\\(FontFamilyMap\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs","lineNumber":367,"sourceCode":"        private void RangeCheck(int index)\n        {\n            ArgumentOutOfRangeException.ThrowIfNegative(index);\n            ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, _count);\n        }\n\n        private void VerifyChangeable()\n        {\n            if (_fontInfo == null)\n                throw new NotSupportedException(SR.General_ObjectIsReadOnly);\n        }\n\n        private FontFamilyMap ConvertValue(object obj)\n        {\n            ArgumentNullException.ThrowIfNull(obj);\n\n            FontFamilyMap familyMap = obj as FontFamilyMap;\n            if (familyMap == null)\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, obj.GetType(), typeof(FontFamilyMap)));\n\n            return familyMap;\n        }\n\n        private class Enumerator : IEnumerator<FontFamilyMap>, SC.IEnumerator\n        {\n            private FontFamilyMap[] _items;\n            private int _count;\n            private int _index;\n            private FontFamilyMap _current;\n\n            internal Enumerator(FontFamilyMap[] items, int count)\n            {\n                _items = items;\n                _count = count;\n                _index = -1;\n                _current = null;\n            }","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs#L349-L385","documentation":"ConvertValue validates that the object supplied to FamilyMapCollection's IList-style Add/Insert is actually a FontFamilyMap. Because the collection implements non-generic IList, the runtime type cannot be checked at compile time, so an invalid object becomes ArgumentException with CannotConvertType naming the actual type and the expected FontFamilyMap.","triggerScenarios":"Calling Add(object) or Insert(index, object) through the non-generic IList interface with something other than a FontFamilyMap, or constructing/inserting wrong-typed items via the collection's internal constructor path.","commonSituations":"Code that treats the collection as IList and adds items read from config, XAML databinding leftovers, or a heterogeneous list of media objects.","solutions":["Only add FontFamilyMap instances: new FontFamilyMap { UnicodeRange = \"...\", Family = \"...\" }.","If the item comes as object, cast or pattern-match before inserting.","If a conversion is intended (e.g. from a string range), convert to FontFamilyMap explicitly before adding."],"exampleFix":"// before\n((IList)collection).Add(\"U+0041-005A; Arial\"); // ArgumentException CannotConvertType\n// after\nvar map = new FontFamilyMap { UnicodeRange = \"U+0041-005A\", Family = \"Arial\" };\n((IList)collection).Add(map);","handlingStrategy":"type-guard","validationCode":"if (obj is not FontFamilyMap) throw new ArgumentException($\"Expected FontFamilyMap, got {obj?.GetType().Name}\");","typeGuard":"static bool IsFamilyMap(object o) => o is FontFamilyMap;","tryCatchPattern":"try { ((IList)collection).Add(obj); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"CannotConvertType\")) { /* wrong item type */ }","preventionTips":["Use the generic collection API rather than non-generic IList.","Pattern-match before casting to object-based Add.","Keep item producers strongly typed."],"tags":["wpf","fonts","type-mismatch","invalid-argument"],"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"}