dotnet/wpf · error · ArgumentException

SR.Collection_BadDestArray

Error message

SR.Collection_BadDestArray

What it means

DrawingCollection's ICollection.CopyTo catches the InvalidCastException thrown by array.SetValue when an element cannot be stored in the destination array, and rethrows it as ArgumentException with SR.Collection_BadDestArray naming the collection type. The destination array's element type must be compatible with Drawing.

Solutions

  1. Use a Drawing[] as the destination array.
  2. Use the strongly typed CopyTo(Drawing[] array, int index) overload instead of the ICollection one.
  3. Check array.GetType().GetElementType().IsAssignableFrom(typeof(Drawing)) before copying.

Example fix

// before
var dest = new ImageSource[coll.Count];
((ICollection)coll).CopyTo(dest, 0); // ArgumentException: Collection_BadDestArray
// after
var dest = new Drawing[coll.Count];
coll.CopyTo(dest, 0);
Defensive patterns

Strategy: type-guard

Validate before calling

if (array.GetType().GetElementType() is not Type et || !typeof(Drawing).IsAssignableFrom(et))
    throw new ArgumentException("Destination array element type must be Drawing-compatible.");

Type guard

static bool CanHoldDrawings(Array a) =>
    a != null && a.Rank == 1 && a.GetType().GetElementType() is Type t && typeof(Drawing).IsAssignableFrom(t);

Try / catch

try { ((ICollection)coll).CopyTo(dest, 0); }
catch (ArgumentException ex) when (ex.Message.Contains("Collection_BadDestArray"))
{ /* reallocate as Drawing[] and retry */ }

Prevention

When it happens

Trigger: Calling ((ICollection)drawingCollection).CopyTo(array, index) with an array whose element type cannot hold Drawing instances (e.g. an ImageSource[] if the elements are not ImageSource, a Brush[], or any unrelated type array).

Common situations: Copying a DrawingCollection into an array typed for a related-but-different media type (ImageSource[], Visual[]) after refactoring; generic copy helpers constructing mistyped arrays; interop layers converting collection types.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DrawingCollection.cs:429

            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)