AvaloniaUI/Avalonia · error · ArgumentException
Invalid array type.
Error message
Invalid array type.
What it means
Centralized ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType throws ArgumentException "Invalid array type." It is reached from the non-generic ICollection.CopyTo on both PooledList<T> and PooledStack<T> inside a `catch (ArrayTypeMismatchException)` block: Array.Copy throws ArrayTypeMismatchException when the destination array's element type is not compatible with T, and this helper rewraps it as a clearer ArgumentException.
Source
Thrown at src/Avalonia.Base/Collections/Pooled/ThrowHelper.cs:282
throw new NotSupportedException();
}
[DoesNotReturn]
internal static void ThrowAggregateException(List<Exception> exceptions)
{
throw new AggregateException(exceptions);
}
[DoesNotReturn]
internal static void ThrowOutOfMemoryException()
{
throw new OutOfMemoryException();
}
[DoesNotReturn]
internal static void ThrowArgumentException_Argument_InvalidArrayType()
{
throw new ArgumentException("Invalid array type.");
}
[DoesNotReturn]
internal static void ThrowInvalidOperationException_InvalidOperation_EnumNotStarted()
{
throw new InvalidOperationException("Enumeration has not started.");
}
[DoesNotReturn]
internal static void ThrowInvalidOperationException_InvalidOperation_EnumEnded()
{
throw new InvalidOperationException("Enumeration has ended.");
}
[DoesNotReturn]
internal static void ThrowInvalidOperationException_EnumCurrent(int index)
{
throw GetInvalidOperationException_EnumCurrent(index);View on GitHub (pinned to 11c5427268)
Solutions
- Use a correctly typed array: `var arr = new T[list.Count]; ((ICollection)list).CopyTo(arr, 0);`.
- Prefer the generic CopyTo(T[], int) overload, which is type-checked at compile time.
- When using the non-generic interface, validate `array.GetType().GetElementType()` is compatible with T.
Example fix
// before object[] arr = new object[list.Count]; ((ICollection)list).CopyTo(arr, 0); // type mismatch // after T[] arr = new T[list.Count]; list.CopyTo(arr, 0);
Defensive patterns
Strategy: type-guard
Validate before calling
var arr = new T[list.Count]; list.CopyTo(arr, 0); // compile-time type-safe, avoids the non-generic path
Type guard
static bool IsCompatibleArray<T>(Array a)
=> a is not null && a.Rank == 1
&& typeof(T).IsAssignableFrom(a.GetType().GetElementType()); Prevention
- Prefer the generic CopyTo(T[], int) over ICollection.CopyTo.
- When using the non-generic interface, verify the element type matches T.
- Avoid object[] as a destination for value-typed collections.
When it happens
Trigger: Calling `((ICollection)list).CopyTo(wrongTypeArray, index)` where the array element type is not assignment-compatible with T — e.g. copying a PooledList<string> into an int[], or a PooledList<Derived> into a Base[] that the runtime rejects (covariance edge cases).
Common situations: Non-generic code passing an object[]/wrongly-typed array through ICollection.CopyTo; reflection or dynamic typing producing a mismatched array type; covariance surprises with reference arrays.
Related errors
- Destination span is shorter than the list to be copied.
- array
- Stack was empty.
- Destination too short.
- Overlap alignment mismatch.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/25a194f79eff8c51.
Report an issue: GitHub.