dotnet/wpf · error · ArgumentException
SR.Argument_InvalidArrayType
Error message
SR.Argument_InvalidArrayType
What it means
For non-T[] destination arrays the library checks type compatibility: it throws ArgumentException with SR.Argument_InvalidArrayType when neither the array's element type nor T is assignable to the other. E.g., copying WeakReference elements into an int[] cannot work because no conversion exists between the element types.
Solutions
- Pass an array whose element type matches T or is assignable from/to T (typically T[] or object[])
- Fix the destination array declaration to use the correct element type
- Guard with targetType.IsAssignableFrom(sourceType) before calling
Example fix
// before string[] target = new string[collection.Count]; ((ICollection)collection).CopyTo(target, 0); // after object[] target = new object[collection.Count]; ((ICollection)collection).CopyTo(target, 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("Array element type incompatible with collection element type."); Type guard
bool IsCompatibleArrayType(Array a) { var t = a.GetType().GetElementType(); return t != null && (t.IsAssignableFrom(typeof(T)) || typeof(T).IsAssignableFrom(t)); } Try / catch
try { ((ICollection)collection).CopyTo(array, index); } catch (ArgumentException e) when (e.Message.Contains("array type")) { object[] boxed = new object[collection.Count]; ((ICollection)collection).CopyTo(boxed, index); } Prevention
- Match the destination element type to the collection's T
- Use object[] when copying through the non-generic interface
- Add type checks in generic copy utilities
When it happens
Trigger: Calling ICollection.CopyTo with an array whose element type is unrelated to T (e.g., string[] when T is WeakReference).
Common situations: Generic copy helpers that pass object[]-like buffers typed incorrectly; refactoring changed T without updating the destination array type.
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
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Format(SR.CannotConvertType, typeof(FamilyTypeface[])…
- SR.Format(SR.CannotConvertType, typeof(SC.DictionaryEntry)…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a1494411fd5a4776.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs:177
IList<T> dlist = CreateDereferencedList();
if (array is T[] items)
{
dlist.CopyTo(items, index);
}
else
{
//
// Catch the obvious case assignment will fail.
// We can found 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)))
{
//ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
throw new ArgumentException(SR.Argument_InvalidArrayType);
}
//
// 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)
{
//ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
throw new ArgumentException(SR.Argument_InvalidArrayType);
}
int count = dlist.Count;
try
{
for (int i = 0; i < count; i++)
{
objects[index++] = dlist[i];View on GitHub (pinned to 81131a70a4)