AvaloniaUI/Avalonia · error · ArgumentException

Invalid array type

Error message

Invalid array type

What it means

ArgumentException ('Invalid array type') thrown by AvaloniaList.ICollection.CopyTo when the destination array's element type is neither assignable from the list's element type T nor vice-versa. The implementation only supports arrays whose element type is type-compatible with T.

Source

Thrown at src/Avalonia.Base/Collections/AvaloniaList.cs:700

            }

            if (array is T[] tArray)
            {
                _inner.CopyTo(tArray, index);
            }
            else
            {
                //
                // Catch the obvious case assignment will fail.
                // We can't find all possible problems by doing the check though.
                // For example, if the element type of the Array is derived from T,
                // we can't figure out if we can successfully copy the element beforehand.
                //
                Type targetType = array.GetType().GetElementType()!;
                Type sourceType = typeof(T);
                if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType)))
                {
                    throw new ArgumentException("Invalid array type");
                }

                //
                // We can't cast array of value type to object[], so we don't support
                // widening of primitive types here.
                //
                if (array is not object?[] objects)
                {
                    throw new ArgumentException("Invalid array type");
                }

                int count = _inner.Count;
                try
                {
                    for (int i = 0; i < count; i++)
                    {
                        objects[index++] = _inner[i];
                    }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Allocate the destination array with the exact element type T: new T[list.Count].
  2. If a base type is required, ensure it is a type T is assignable to (e.g. object[] for reference types).
  3. Validate targetType.IsAssignableFrom(typeof(T)) || typeof(T).IsAssignableFrom(targetType) before copying.

Example fix

// before
var arr = new int[list.Count];
((ICollection)stringList).CopyTo(arr, 0);

// after
var arr = new string[list.Count];
((ICollection)stringList).CopyTo(arr, 0);
Defensive patterns

Strategy: type-guard

Validate before calling

var targetType = array.GetType().GetElementType();
if (!(targetType.IsAssignableFrom(typeof(T)) || typeof(T).IsAssignableFrom(targetType))) throw new ArgumentException("Incompatible array element type.");

Type guard

static bool IsElementTypeCompatible(Array a) {
    var t = a.GetType().GetElementType()!;
    return t.IsAssignableFrom(typeof(T)) || typeof(T).IsAssignableFrom(t);
}

Prevention

When it happens

Trigger: Passing an array whose element type is unrelated to T, e.g. copying an AvaloniaList<string> into an int[] or a DateTime[]; element types with no inheritance relationship in either direction.

Common situations: Reflection/dynamic code that allocates the destination via a runtime type unrelated to T; mis-typed buffer pools; serializers guessing the element type.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/2fdedf21b53795b9. Report an issue: GitHub.