dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

Thrown by GeneralTransformCollection.CopyTo when the target array is multidimensional (array.Rank != 1). The WPF collection classes only support copying into one-dimensional arrays, matching standard BCL ICollection.CopyTo behavior.

Solutions

  1. Pass a one-dimensional GeneralTransform[] sized to collection.Count plus the desired index offset
  2. Check array.Rank == 1 before calling CopyTo and allocate a flat array if not
  3. Marshal data manually with nested loops if a multidimensional layout is required

Example fix

// before
var matrix = new GeneralTransform[2, 2];
collection.CopyTo(0, matrix);
// after
var flat = new GeneralTransform[collection.Count];
collection.CopyTo(0, flat);
Defensive patterns

Strategy: validation

Validate before calling

if (target == null) throw new ArgumentNullException(nameof(target));
if (target.Rank != 1) throw new ArgumentException("Destination array must be one-dimensional", nameof(target));
if (index + collection.Count > target.Length) throw new ArgumentException("Destination array too small", nameof(target));

Type guard

static bool IsUsableCopyTarget(Array a, int index, int count) => a is { Rank: 1 } && index >= 0 && index + count <= a.Length;

Try / catch

try { collection.CopyTo(0, array); } catch (ArgumentException ex) when (array.Rank != 1) { /* fall back to flat array */ }

Prevention

When it happens

Trigger: Calling CopyTo(int index, System.Array array) with a 2D array such as new GeneralTransform[3,3], or any array created with Array.CreateInstance(type, lengths, lowerBounds) with more than one dimension.

Common situations: Copying a collection into a matrix-shaped buffer, reusing a rectangular array that was used for tabular data, or generically calling ICollection.CopyTo via reflection with an arbitrary Array.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/GeneralTransformCollection.cs:402

        #endregion

        #region ICollection

        void ICollection.CopyTo(Array array, int index)
        {
            ReadPreamble();

            ArgumentNullException.ThrowIfNull(array);

            // This will not throw in the case that we are copying
            // from an empty collection.  This is consistent with the
            // BCL Collection implementations. (Windows 1587365)
            ArgumentOutOfRangeException.ThrowIfNegative(index);
            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _collection.Count);

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

            // Elsewhere in the collection we throw an AE when the type is
            // bad so we do it here as well to be consistent
            try
            {
                int count = _collection.Count;
                for (int i = 0; i < count; i++)
                {
                    array.SetValue(_collection[i], index + i);
                }
            }
            catch (InvalidCastException e)
            {
                throw new ArgumentException(SR.Format(SR.Collection_BadDestArray, this.GetType().Name), e);
            }
        }

View on GitHub (pinned to 81131a70a4)