dotnet/wpf · error · ArgumentException

SR.Collection_BadDestArray

Error message

SR.Collection_BadDestArray

What it means

Thrown by GeneralTransformCollection.CopyTo when an element in the collection cannot be stored into the destination array because its type is not assignable to the array's element type. The original InvalidCastException is preserved as the inner exception.

Solutions

  1. Ensure the destination array element type is GeneralTransform or a base type that accepts all collection elements
  2. Catch ArgumentException and inspect the inner InvalidCastException for the offending element type
  3. Copy via OfType<T>() filtering if the destination must be a subclass array

Example fix

// before
var scales = new ScaleTransform[collection.Count];
((ICollection)collection).CopyTo(scales, 0);
// after
var transforms = new GeneralTransform[collection.Count];
((ICollection)collection).CopyTo(transforms, 0);
Defensive patterns

Strategy: validation

Validate before calling

var elemType = destination.GetType().GetElementType();
if (elemType != null && !elemType.IsAssignableFrom(typeof(GeneralTransform)))
    throw new ArgumentException("Element type cannot hold GeneralTransform items");

Type guard

static bool CanHoldTransforms(Array a) => a.GetType().GetElementType()?.IsAssignableFrom(typeof(GeneralTransform)) == true;

Try / catch

try { ((ICollection)collection).CopyTo(destination, 0); } catch (ArgumentException ex) { var inner = ex.InnerException as InvalidCastException; /* inspect and fix element type */ }

Prevention

When it happens

Trigger: Calling CopyTo(0, array) where array is, e.g., an object[] pre-typed as RotateTransform[] but the collection contains a GeneralTransform of an incompatible concrete type, or an array with element type that does not accept GeneralTransform instances.

Common situations: Copying into a strongly typed subclass array (e.g. ScaleTransform[]) when the collection holds other transform types, or copying through the non-generic ICollection.CopyTo with an object[] from legacy code that is actually typed differently.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/GeneralTransformCollection.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)