dotnet/wpf · error · ArgumentException

SR.Format(SR.Collection_BadDestArray, this.GetType().Name)

Error message

SR.Format(SR.Collection_BadDestArray, this.GetType().Name)

What it means

GeneralTransform3DCollection.CopyTo wraps array writes in a try/catch for InvalidCastException and rethrows it as ArgumentException with Collection_BadDestArray. It means the destination array's element type cannot hold a GeneralTransform3D (e.g. array of a wrong or incompatible type). The library converts this low-level cast failure into a clearer argument error naming the collection type.

Solutions

  1. Allocate the destination array as GeneralTransform3D[] with length >= collection.Count.
  2. If you need a different element type, copy via LINQ: var arr = collection.Cast<TargetType>().ToArray() after verifying convertibility.
  3. Check index + collection.Count stays within the destination array bounds; copy only a compatible subrange.

Example fix

// before
var arr = new Transform3D[collection.Count];
collection.CopyTo(arr, 0);
// after
var arr = new GeneralTransform3D[collection.Count];
collection.CopyTo(arr, 0);
Defensive patterns

Strategy: validation

Validate before calling

// before CopyTo
if (destArray == null) throw new ArgumentNullException(nameof(destArray));
if (destArray is GeneralTransform3D[] typed && typed.Length >= collection.Count && index + collection.Count <= destArray.Length)
{
    collection.CopyTo(destArray, index);
}

Type guard

bool IsCompatibleDestArray(Array a, int count) => a is GeneralTransform3D[] && a.Length >= count;

Try / catch

try { collection.CopyTo(destArray, index); }
catch (ArgumentException ex) when (ex.Message.Contains("array"))
{
    // recreate as GeneralTransform3D[] and retry
    var typed = new GeneralTransform3D[collection.Count];
    collection.CopyTo(typed, 0);
}

Prevention

When it happens

Trigger: Calling CopyTo on a GeneralTransform3DCollection with an Array whose element type is not GeneralTransform3D or a compatible assignable type (e.g. Transform[], object[] is fine, but Transform3D[] or a struct array is not).

Common situations: Copying between similarly named collections (2D vs 3D transform collections), legacy code refactored from Transform to GeneralTransform3D, or generic serialization code that allocates arrays from reflected metadata.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/GeneralTransform3DCollection.cs:415

            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)