dotnet/wpf · error · ArgumentException

SR.Collection_CopyTo_ArrayCannotBeMultidimensional

Error message

SR.Collection_CopyTo_ArrayCannotBeMultidimensional

What it means

FamilyTypefaceCollection.CopyTo supports only single-dimensional arrays. CopyItems throws ArgumentException with Collection_CopyTo_ArrayCannotBeMultidimensional when the destination array's Rank is not 1, matching ICollection.CopyTo conventions.

Solutions

  1. Copy into a one-dimensional FamilyTypeface[] instead.
  2. Flatten the multidimensional array copy by iterating with nested loops and adding items individually.
  3. Use LINQ: FamilyTypeface[] arr = collection.ToArray();

Example fix

// before
var grid = new FamilyTypeface[2, 5];
collection.CopyTo(grid, 0); // ArgumentException
// after
var flat = new FamilyTypeface[collection.Count];
collection.CopyTo(flat, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null) throw new ArgumentNullException(nameof(array));
if (array.Rank != 1) throw new ArgumentException("CopyTo requires a single-dimensional array.");

Try / catch

try { collection.CopyTo(array, 0); }
catch (ArgumentException ex) when (ex.Message.Contains("Multidimensional")) { array = new FamilyTypeface[collection.Count]; collection.CopyTo(array, 0); }

Prevention

When it happens

Trigger: Calling CopyTo with a multidimensional array (e.g. FamilyTypeface[,] or Array.CreateInstance with rank 2) as the destination.

Common situations: Generic copying utilities that allocate rectangular arrays for collections, or interop code that assumes multidimensional arrays are supported.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

        }

        private FamilyTypeface ConvertValue(object obj)
        {
            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);
            }
        }

View on GitHub (pinned to 81131a70a4)