dotnet/wpf · error · ArgumentException

SR.Collection_CopyTo_ArrayCannotBeMultidimensional

Error message

SR.Collection_CopyTo_ArrayCannotBeMultidimensional

What it means

The explicit ICollection.CopyTo(Array, int) implementation of FamilyMapCollection rejects multidimensional arrays: array.Rank != 1 throws ArgumentException with Collection_CopyTo_ArrayCannotBeMultidimensional, since it performs a flat Array.Copy.

Solutions

  1. Pass a one-dimensional array (or a jagged array's inner 1D arrays) as the destination.
  2. Flatten multidimensional buffers before copying, or use OfType<FontFamilyMap>().ToArray() on the collection.
  3. Add a Rank check in helper code before dispatching to ICollection.CopyTo.

Example fix

// before
var dest = new FontFamilyMap[10, 10];
((ICollection)familyMaps).CopyTo(dest, 0);
// after
var dest = new FontFamilyMap[familyMaps.Count];
((ICollection)familyMaps).CopyTo(dest, 0);
Defensive patterns

Strategy: type-guard

Validate before calling

if (array.Rank != 1) array = new FontFamilyMap[col.Count];

Type guard

static bool IsFlatAssignableArray(Array a) => a.Rank == 1 && a.GetType().GetElementType().IsAssignableFrom(typeof(FontFamilyMap));

Try / catch

try { ((ICollection)col).CopyTo(array, index); }
catch (ArgumentException) { /* allocate 1D array and retry */ }

Prevention

When it happens

Trigger: Calling ((ICollection)familyMapCollection).CopyTo with a 2D (or higher-rank) Array instance as the destination.

Common situations: Generic copy helpers that take Array and allocate new int[rows, cols]-style buffers, or code converting from multidimensional grid storage to the collection.

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/5cfeb0fc2a74f0a8. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs:91

        {
            ArgumentNullException.ThrowIfNull(array);

            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)
                Array.Copy(_items, 0, array, index, _count);
        }

        void SC.ICollection.CopyTo(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)
                Array.Copy(_items, 0, array, index, _count);
        }

        bool SC.ICollection.IsSynchronized
        {
            get { return false; }

View on GitHub (pinned to 81131a70a4)