dotnet/wpf · error · ArgumentException

SR.Collection_BadDestArray

Error message

SR.Collection_BadDestArray

What it means

Point3DCollection.ICollection.CopyTo wraps the SetValue loop in a try/catch and rethrows InvalidCastException as ArgumentException(SR.Collection_BadDestArray) naming the collection type. This occurs when the destination array's element type cannot hold a Point3D value.

Solutions

  1. Use a Point3D[] destination array or the generic CopyTo(Point3D[], int) overload
  2. Verify the destination array element type is Point3D or object
  3. Catch ArgumentException and inspect InnerException (InvalidCastException) when handling untyped arrays

Example fix

// before
Array dest = new Vector3D[count];
((ICollection)collection).CopyTo(dest, 0); // throws
// after
var dest = new Point3D[count];
collection.CopyTo(dest, 0);
Defensive patterns

Strategy: type-guard

Validate before calling

if (destArray.GetType().GetElementType() != typeof(Point3D) && destArray.GetType().GetElementType() != typeof(object)) throw new ArgumentException("bad destination array type");

Type guard

bool CanHoldPoint3D(Array a) { var t = a.GetType().GetElementType(); return t == typeof(Point3D) || t == typeof(object); }

Try / catch

try { ((ICollection)col).CopyTo(arr, i); }
catch (ArgumentException ex) when (ex.InnerException is InvalidCastException) { /* wrong element type; allocate Point3D[] */ }

Prevention

When it happens

Trigger: Calling the non-generic ICollection.CopyTo with an array whose element type is incompatible, e.g. CopyTo(new Vector3D[n], 0) or CopyTo(new object[...] assigned wrongly / Array of non-assignable type passed as Array).

Common situations: Code using the non-generic IList/ICollection interface with arrays of the wrong element type; refactoring changed Point3D to another vector type but not the buffer.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/Point3DCollection.cs:380

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

        bool ICollection.IsSynchronized
        {
            get
            {
                ReadPreamble();

                return IsFrozen || Dispatcher != null;
            }
        }

        object ICollection.SyncRoot
        {
            get
            {
                ReadPreamble();

View on GitHub (pinned to 81131a70a4)