dotnet/wpf · error · ArgumentException

SR.Collection_BadDestArray

Error message

SR.Collection_BadDestArray

What it means

PathSegmentCollection.CopyTo throws Collection_BadDestArray (as an ArgumentException) when one or more elements in the collection cannot be cast to the destination array's element type. The copy is performed via array.SetValue, and an InvalidCastException during that write is converted into this argument error, with the original cast failure as the InnerException.

Solutions

  1. Use a PathSegment[] (or PathSegment-compatible) array as the destination of CopyTo.
  2. Check InnerException (InvalidCastException) to identify the offending element type.
  3. Copy via the generic ICollection<PathSegment>.CopyTo or LINQ ToArray<PathSegment>() instead.
  4. Ensure the array is large enough: index + Count must be within array bounds.

Example fix

// before
var arr = new object[10];
collection.CopyTo((Array)arr, 0);
// after
var arr = new PathSegment[collection.Count];
collection.CopyTo(arr, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null) throw new ArgumentNullException(nameof(array));
if (array.Rank != 1 || index < 0 || index + collection.Count > array.Length) throw new ArgumentException("Bad destination array");
if (array.GetType().GetElementType() != null && !typeof(PathSegment).IsAssignableFrom(array.GetType().GetElementType())) throw new ArgumentException("Array element type must be PathSegment-compatible");

Type guard

static bool IsCompatibleDestArray(Array a) => a.Rank == 1 && typeof(PathSegment).IsAssignableFrom(a.GetType().GetElementType());

Try / catch

try { collection.CopyTo(arr, 0); }
catch (ArgumentException ex) when (ex.InnerException is InvalidCastException) { /* fall back to typed copy */ }

Prevention

When it happens

Trigger: Calling CopyTo on a PathSegmentCollection with an array whose element type is not PathSegment and is not assignment-compatible (e.g. object[] is fine, but double[] or string[] is not) while the collection is non-empty.

Common situations: Copying into arrays typed as a base-of-PathSegment-less type, legacy code using non-generic ICollection.CopyTo with ArrayList or incorrectly typed buffers, or refactoring that changed the array type without updating the source collection type.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PathSegmentCollection.cs:417

            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)