{"record":{"id":"c219071ae426f5bd","repo":"dotnet/wpf","slug":"sr-format-sr-cannotconverttype-typeof-familytypeface","errorCode":null,"errorMessage":"SR.Format(SR.CannotConvertType, typeof(FamilyTypeface), elementType)","messagePattern":"SR\\.Format\\(SR\\.CannotConvertType, typeof\\(FamilyTypeface\\), elementType\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs","lineNumber":95,"sourceCode":"                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"index\", \"array\"));\n\n            if (_count > array.Length - index)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\"));\n\n            if (_count != 0)\n                Array.Copy(_items, 0, array, index, _count);\n        }\n\n        void SC.ICollection.CopyTo(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\n            if (_count > array.Length - index)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\"));\n\n            if (_count != 0)\n                Array.Copy(_items, 0, array, index, _count);\n        }\n\n        bool SC.ICollection.IsSynchronized\n        {\n            get { return false; }\n        }\n\n        object SC.ICollection.SyncRoot\n        {","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs#L77-L113","documentation":"The explicit ICollection.CopyTo(Array, int) of FamilyMapCollection requires the destination element type to accept FontFamilyMap elements; if elementType.IsAssignableFrom(typeof(FamilyTypeface)) fails (note: the collection's element type check used in this code path), it throws ArgumentException with CannotConvertType naming the source and target types.","triggerScenarios":"Calling ((ICollection)familyMapCollection).CopyTo with a non-assignable array type such as object[]-typed Array instances created via Array.CreateInstance, or arrays of an unrelated type like FontFamily[] or int[].","commonSituations":"Reflection-based serialization writing into Array.CreateInstance(typeof(...), n) buffers, or mismatched generic/legacy collection bridges.","solutions":["Use a FontFamilyMap[] (or a base type/interface array the element is assignable to) as the destination.","Check array.GetType().GetElementType().IsAssignableFrom(typeof(FontFamilyMap)) before copying.","Use ToArray()/Cast<T>() instead of the non-generic ICollection path when types are uncertain."],"exampleFix":"// before\nArray dest = Array.CreateInstance(typeof(int), familyMaps.Count);\n((ICollection)familyMaps).CopyTo(dest, 0);\n// after\nvar dest = new FontFamilyMap[familyMaps.Count];\n((ICollection)familyMaps).CopyTo(dest, 0);","handlingStrategy":"type-guard","validationCode":"bool ok = array.GetType().GetElementType().IsAssignableFrom(typeof(FontFamilyMap));","typeGuard":"static bool CanHoldFamilyMaps(Array a) => a.GetType().GetElementType()?.IsAssignableFrom(typeof(FontFamilyMap)) == true;","tryCatchPattern":"try { ((ICollection)col).CopyTo(array, index); }\ncatch (ArgumentException) { /* use correctly typed array */ }","preventionTips":["Use FontFamilyMap[] destinations rather than Array.CreateInstance buffers","Keep generic overloads over non-generic ICollection paths"],"tags":["wpf","fonts","collections","type-mismatch"],"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"}