dotnet/wpf · error · ArgumentException

Collection_BadRank

Error message

Collection_BadRank

What it means

GlyphTypeface's ICollection<KeyValuePair<ushort,double>> CopyTo implementation throws ArgumentException with resource 'Collection_BadRank' when the destination array is multi-dimensional. .NET collection copy contracts only support single-dimensional (rank 1) arrays, so a jagged rank check is enforced before copying advance ratios.

Solutions

  1. Use a single-dimensional array as the destination (new KeyValuePair<ushort,double>[Count])
  2. Flatten multidimensional data into a 1D array before calling CopyTo
  3. Check array.Rank == 1 before calling CopyTo

Example fix

// before
var metrics = new KeyValuePair<ushort, double>[5, 5];
glyphTypeface.AdvanceMetrics.CopyTo(metrics, 0);
// after
var metrics = new KeyValuePair<ushort, double>[glyphTypeface.AdvanceMetrics.Count];
glyphTypeface.AdvanceMetrics.CopyTo(metrics, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (dest == null) throw new ArgumentNullException(nameof(dest));
if (dest.Rank != 1) throw new ArgumentException("Destination array must be single-dimensional", nameof(dest));
if (arrayIndex < 0 || arrayIndex >= dest.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex));

Type guard

bool CanCopyTo<T>(T[] array) => array != null && array.Rank == 1;

Prevention

When it happens

Trigger: Calling GlyphTypeface.AdvanceMetrics.CopyTo(...) (or the explicit interface CopyTo) with a 2D array (e.g. new double[3,3] cast or a KeyValuePair<ushort,double>[,]) as the destination array.

Common situations: Developers reusing a multidimensional scratch buffer for glyph metrics, or code ported from C++/COM that uses 2D arrays, passing it to a generic ICollection.CopyTo API.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs:1813

            }

            public void Clear()
            {
                throw new NotSupportedException();
            }

            public bool Contains(KeyValuePair<ushort, double> item)
            {
                return ContainsKey(item.Key);
            }

            public void CopyTo(KeyValuePair<ushort, double>[] array, int arrayIndex)
            {
                ArgumentNullException.ThrowIfNull(array);

                if (array.Rank != 1)
                {
                    throw new ArgumentException(SR.Collection_BadRank);
                }

                // The extra "arrayIndex >= array.Length" check in because even if _collection.Count
                // is 0 the index is not allowed to be equal or greater than the length
                // (from the MSDN ICollection docs)
                ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
                ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(arrayIndex, array.Length);
                ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);

                for (ushort i = 0; i < Count; ++i)
                    array[arrayIndex + i] = new KeyValuePair<ushort, double>(i, this[i]);
            }

            public int Count
            {
                get { return _numberOfGlyphs; }
            }

View on GitHub (pinned to 81131a70a4)