dotnet/wpf · error · ArgumentException

SR.Collection_BadDestArray

Error message

SR.Collection_BadDestArray

What it means

PointCollection.CopyTo throws ArgumentException (wrapping InvalidCastException) when the destination array cannot hold Point elements, i.e. WPF cannot cast every collected value into the destination array's element type. The library catches the InvalidCastException from array.SetValue and rethrows it as ArgumentException with Collection_BadDestArray, naming the collection type. It signals the destination array's type is incompatible with the collection's element type, not a size problem.

Solutions

  1. Allocate the destination array as Point[] (or a type Point is assignable to) before calling CopyTo.
  2. If you need a different element type, project first: points.Select(p => p.ToString()).ToArray() then copy.
  3. Verify array.GetElementType() compatibility before copying (see validation code).

Example fix

// before
Array dest = new string[points.Count];
points.CopyTo(dest, 0); // ArgumentException Collection_BadDestArray
// after
var dest = new Point[points.Count];
points.CopyTo(dest, 0);
Defensive patterns

Strategy: validation

Validate before calling

static bool CanCopyTo(PointCollection c, Array dest) => dest != null && (dest.GetElementType()?.IsAssignableFrom(typeof(Point)) ?? false);

Type guard

static bool IsPointArray(Array a) => a?.GetElementType() == typeof(Point);

Try / catch

try { points.CopyTo(dest, 0); }
catch (ArgumentException ex) when (ex.Message.Contains("Collection_BadDestArray")) { /* recreate dest as Point[] */ }

Prevention

When it happens

Trigger: Calling PointCollection.CopyTo (explicit ICollection.CopyTo or the generic overload path that shares it) with an array whose element type is not Point and not assignable from Point, e.g. copying a PointCollection into an object[] whose runtime type disallows Point, or a string[]/int[].

Common situations: Passing a loosely typed Array variable that was actually allocated as a non-Point array; marshaling old code that used ArrayList-style copies into typed arrays; refactoring that changed the destination array's element type without updating the CopyTo call.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PointCollection.cs:382

            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)