dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

Visual3DCollection.CopyTo validates the destination array and throws ArgumentException(SR.Collection_BadRank) when array.Rank != 1, i.e. the target is a multi-dimensional array. IList.CopyTo in WPF collections only supports single-dimensional (rank-1) arrays.

Solutions

  1. Pass a single-dimensional Visual3D[] sized to Count (or larger) as the destination
  2. If a multidimensional result is needed, copy into a 1D array first, then re-shape manually
  3. Guard with array.Rank == 1 before calling CopyTo

Example fix

// before
var arr = Array.CreateInstance(typeof(Visual3D), new[] { n, 1 });
collection.CopyTo(arr, 0);
// after
var arr = new Visual3D[collection.Count];
collection.CopyTo(arr, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (dest is Array a && a.Rank == 1 && a.Length - index >= collection.Count) collection.CopyTo(a, index);

Type guard

static bool IsValidDest(Visual3DCollection c, Visual3D[] dest, int index) => dest != null && index >= 0 && dest.Length - index >= c.Count;

Try / catch

try { collection.CopyTo(arr, 0); } catch (ArgumentException) { arr = new Visual3D[collection.Count]; collection.CopyTo(arr, 0); }

Prevention

When it happens

Trigger: Calling visual3DCollection.CopyTo(multidimArray, 0) with e.g. a Visual3D[,] destination; passing an array created via Array.CreateInstance with rank > 1.

Common situations: Generic serialization/data-binding code that allocates Array.CreateInstance(typeof(Visual3D), lengths) with multiple dimensions; ported code from collections that accepted multidimensional 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/52545a8298f5ba1c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Visual3DCollection.cs:159

            _collection.CopyTo(array, index);
        }

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

            ArgumentNullException.ThrowIfNull(array);

            // The extra "index >= 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(index);
            ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, array.Length);
            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - 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, "Visual3DCollection"), e);
            }
        }

View on GitHub (pinned to 81131a70a4)