dotnet/wpf · error · ArgumentException

SR.Collection_CopyTo_ArrayCannotBeMultidimensional

Error message

SR.Collection_CopyTo_ArrayCannotBeMultidimensional

What it means

The non-generic ICollection.CopyTo of CharacterMetricsDictionary only supports one-dimensional arrays when the target is not exactly DictionaryEntry[]. If array.Rank != 1 (a multidimensional array such as object[,]), an ArgumentException with Collection_CopyTo_ArrayCannotBeMultidimensional is thrown.

Solutions

  1. Use a one-dimensional array (e.g. DictionaryEntry[] or object[]).
  2. Flatten or slice the multidimensional array into a 1D view before copying.
  3. If you need positional storage, copy into a 1D array and index it manually into the 2D structure.

Example fix

// before
var grid = new object[dict.Count, 2];
((ICollection)dict).CopyTo(grid, 0);
// after
var flat = new object[((ICollection)dict).Count];
((ICollection)dict).CopyTo(flat, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array.Rank != 1)
    throw new ArgumentException("CopyTo requires a one-dimensional array.");

Try / catch

try { ((ICollection)dict).CopyTo(array, index); }
catch (ArgumentException) { /* switch to a 1D array */ }

Prevention

When it happens

Trigger: Passing a 2D+ array (e.g. object[,], DictionaryEntry[,]) to ((ICollection)CharacterMetricsDictionary).CopyTo.

Common situations: Legacy code written against rectangular array buffers; generic helper methods that accept Array and accidentally receive multidimensional instances; binding/serialization frameworks using 2D scratch arrays.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs:196

                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"));

            SC.DictionaryEntry[] typedArray = array as SC.DictionaryEntry[];
            if (typedArray != null)
            {
                // it's an array of the exact type
                foreach (KeyValuePair<int, CharacterMetrics> item in this)
                {
                    typedArray[index++] = new SC.DictionaryEntry(item.Key, item.Value);
                }
            }
            else
            {
                // it's an array of some other type, e.g., object[]; make sure it's one dimensional
                if (array.Rank != 1)
                    throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);

                // make sure the element type is compatible
                Type elementType = array.GetType().GetElementType();
                if (!elementType.IsAssignableFrom(typeof(SC.DictionaryEntry)))
                    throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(SC.DictionaryEntry), elementType));

                foreach (KeyValuePair<int, CharacterMetrics> item in this)
                {
                    array.SetValue(new SC.DictionaryEntry(item.Key, item.Value), index++);
                }
            }
        }

        #endregion

        #region IDictionary members

        /// <summary>

View on GitHub (pinned to 81131a70a4)