{"record":{"id":"33a851a7c1d119ad","repo":"dotnet/wpf","slug":"sr-format-sr-cannotconverttype-typeof-familytypeface-33a851","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/FamilyTypefaceCollection.cs","lineNumber":373,"sourceCode":"            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\n            if (_count > array.Length - index)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\"));\n\n            if (_count != 0)\n            {\n                InitializeItemsFromInnerList();\n                Array.Copy(_items, 0, array, index, _count);\n            }\n        }\n\n        private class Enumerator : IEnumerator<FamilyTypeface>, SC.IEnumerator\n        {\n            private FamilyTypefaceCollection _list;\n            private int _index;","sourceCodeStart":355,"sourceCodeEnd":391,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs#L355-L391","documentation":"CopyTo validates the destination array's element type: it must be assignable from FamilyTypeface. If GetElementType() returns a type that cannot hold FamilyTypeface items, ArgumentException with CannotConvertType is thrown, reporting the expected FamilyTypeface[] and the actual element type.","triggerScenarios":"Calling CopyTo with an array typed as e.g. object[...] — no, object is assignable — precisely with incompatible element types such as FamilyTypeface[][] (jagged), FontFamilyMap[], or string[].","commonSituations":"Passing an array typed for a different collection's item type after a refactor, or a jagged array whose element type is a nested array.","solutions":["Use a FamilyTypeface[] destination array.","Change the array's declared element type to match the collection's item type.","Copy element-by-element with a loop into the correct typed array."],"exampleFix":"// before\nvar dest = new FontFamilyMap[collection.Count];\ncollection.CopyTo(dest, 0); // ArgumentException CannotConvertType\n// after\nvar dest = new FamilyTypeface[collection.Count];\ncollection.CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"var elementType = array.GetType().GetElementType();\nif (elementType == null || !typeof(FamilyTypeface).IsAssignableFrom(elementType)) throw new ArgumentException(\"Array element type must be assignable from FamilyTypeface.\");","typeGuard":null,"tryCatchPattern":"try { collection.CopyTo(array, 0); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"CannotConvertType\")) { /* wrong element type; allocate FamilyTypeface[] */ }","preventionTips":["Declare destinations as FamilyTypeface[].","Avoid jagged arrays (FamilyTypeface[][]) for CopyTo.","Check GetElementType() compatibility in generic copy helpers."],"tags":["wpf","copyto","arrays","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"}