dotnet/wpf · error · ArgumentException

SR.Collection_BadDestArray

Error message

SR.Collection_BadDestArray

What it means

TextEffectCollection.CopyTo throws ArgumentException wrapping InvalidCastException when the destination array cannot accept the elements. Internally it calls array.SetValue for each TextEffect; if the array's element type cannot hold a TextEffect, the runtime throws InvalidCastException which is rethrown as Collection_BadDestArray naming the collection type.

Solutions

  1. Use an array of type TextEffect[] (or a compatible base type) as the destination
  2. Check array.GetType().GetElementType().IsAssignableFrom(typeof(TextEffect)) before calling CopyTo
  3. Verify index plus collection count fits within the array bounds (out-of-range also throws here)

Example fix

// before
var effects = new TextEffectCollection();
var bad = new object[0]; // actually must be assignable
effects.CopyTo(bad, 0);
// after
var effects = new TextEffectCollection();
var dest = new TextEffect[effects.Count];
effects.CopyTo(dest, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null) throw new ArgumentNullException(nameof(array));
if (array.GetType().GetElementType() != null && !array.GetType().GetElementType().IsAssignableFrom(typeof(TextEffect)))
    throw new ArgumentException("Destination array element type must be TextEffect-compatible.");

Type guard

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

Prevention

When it happens

Trigger: Calling CopyTo on an array whose element type is not TextEffect or a compatible type (e.g. object[] works, but a Shape[] or StreamGeometry[] does not).

Common situations: Copying a WPF collection into a mismatched typed array after refactoring element types; passing a shared generic buffer of the wrong type; interop code that assumed all collections were object arrays.

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

Appendix: source

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