dotnet/wpf · error · ArgumentException

SR.Format(SR.CannotConvertType, typeof(FamilyTypeface[])…

Error message

SR.Format(SR.CannotConvertType, typeof(FamilyTypeface[]), elementType)

What it means

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.

Solutions

  1. Use a FamilyTypeface[] destination array.
  2. Change the array's declared element type to match the collection's item type.
  3. Copy element-by-element with a loop into the correct typed array.

Example fix

// before
var dest = new FontFamilyMap[collection.Count];
collection.CopyTo(dest, 0); // ArgumentException CannotConvertType
// after
var dest = new FamilyTypeface[collection.Count];
collection.CopyTo(dest, 0);
Defensive patterns

Strategy: validation

Validate before calling

var elementType = array.GetType().GetElementType();
if (elementType == null || !typeof(FamilyTypeface).IsAssignableFrom(elementType)) throw new ArgumentException("Array element type must be assignable from FamilyTypeface.");

Try / catch

try { collection.CopyTo(array, 0); }
catch (ArgumentException ex) when (ex.Message.Contains("CannotConvertType")) { /* wrong element type; allocate FamilyTypeface[] */ }

Prevention

When it happens

Trigger: 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[].

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/33a851a7c1d119ad. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs:373

            ArgumentNullException.ThrowIfNull(obj);

            FamilyTypeface familyTypeface = obj as FamilyTypeface;
            if (familyTypeface == null)
                throw new ArgumentException(SR.Format(SR.CannotConvertType, obj.GetType(), typeof(FamilyTypeface)));

            return familyTypeface;
        }

        private void CopyItems(Array array, int index)
        {
            ArgumentNullException.ThrowIfNull(array);

            if (array.Rank != 1)
                throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);

            Type elementType = array.GetType().GetElementType();
            if (!elementType.IsAssignableFrom(typeof(FamilyTypeface)))
                throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(FamilyTypeface[]), elementType));

            if (index >= array.Length)
                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array"));

            if (_count > array.Length - index)
                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, "array"));

            if (_count != 0)
            {
                InitializeItemsFromInnerList();
                Array.Copy(_items, 0, array, index, _count);
            }
        }

        private class Enumerator : IEnumerator<FamilyTypeface>, SC.IEnumerator
        {
            private FamilyTypefaceCollection _list;
            private int _index;

View on GitHub (pinned to 81131a70a4)