{"record":{"id":"65424d8c32ba8590","repo":"dotnet/wpf","slug":"sr-format-sr-cannotconverttype-obj-gettype-typeof-65424d","errorCode":null,"errorMessage":"SR.Format(SR.CannotConvertType, obj.GetType(), typeof(FamilyTypeface))","messagePattern":"SR\\.Format\\(SR\\.CannotConvertType, obj\\.GetType\\(\\), typeof\\(FamilyTypeface\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs","lineNumber":359,"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 (_innerList != null)\n                throw new NotSupportedException(SR.General_ObjectIsReadOnly);\n        }\n\n        private FamilyTypeface ConvertValue(object obj)\n        {\n            ArgumentNullException.ThrowIfNull(obj);\n\n            FamilyTypeface familyTypeface = obj as FamilyTypeface;\n            if (familyTypeface == null)\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, obj.GetType(), typeof(FamilyTypeface)));\n\n            return familyTypeface;\n        }\n\n        private void CopyItems(Array array, int index)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n\n            if (array.Rank != 1)\n                throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);\n\n            Type elementType = array.GetType().GetElementType();\n            if (!elementType.IsAssignableFrom(typeof(FamilyTypeface)))\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(FamilyTypeface[]), elementType));\n\n            if (index >= array.Length)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"index\", \"array\"));\n","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs#L341-L377","documentation":"ConvertValue enforces that items passed to FamilyTypefaceCollection's IList-style Add/Insert are FamilyTypeface instances. Any other object raises ArgumentException with CannotConvertType, reporting the supplied object's actual type and the expected FamilyTypeface type.","triggerScenarios":"Calling Add(object) or Insert(index, object) via the non-generic IList interface with a non-FamilyTypeface object, or adding items via the collection's constructor path with wrong types.","commonSituations":"Reflection or databinding code that inserts loosely-typed objects (strings, generic glyph descriptions) into the typeface collection.","solutions":["Only insert FamilyTypeface instances.","Pattern-match/cast the object and construct a FamilyTypeface with the intended values before inserting.","Fix upstream producers (binding, reflection) so they emit FamilyTypeface objects."],"exampleFix":"// before\n((IList)collection).Add(FontWeights.Bold); // ArgumentException CannotConvertType\n// after\nvar tf = new FamilyTypeface { Weight = FontWeights.Bold };\n((IList)collection).Add(tf);","handlingStrategy":"type-guard","validationCode":"if (obj is not FamilyTypeface) throw new ArgumentException($\"Expected FamilyTypeface, got {obj?.GetType().Name}\");","typeGuard":"static bool IsFamilyTypeface(object o) => o is FamilyTypeface;","tryCatchPattern":"try { ((IList)collection).Add(obj); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"CannotConvertType\")) { /* wrong item type */ }","preventionTips":["Use the strongly-typed Add(FamilyTypeface) overloads.","Pattern-match loosely typed inputs before inserting.","Ensure bindings/reflection emit FamilyTypeface instances."],"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"}