dotnet/wpf · error · ArgumentException
SR.Collection_BadDestArray
Error message
SR.Collection_BadDestArray
What it means
CopyTo wraps InvalidCastException and rethrows ArgumentException (SR.Collection_BadDestArray) when an element cannot be stored into the destination array, i.e. the array's element type is not assignment-compatible with Transform3D.
Solutions
- Use an array whose element type is Transform3D (or object), or use the generic CopyTo(Transform3D[], int) overload.
- Prefer iterating with foreach into a correctly typed buffer.
- Verify the array element type with typeof(Transform3D).IsAssignableFrom(arr.GetType().GetElementType()) before calling.
Example fix
// before var target = new Visual3D[collection.Count]; ((ICollection)collection).CopyTo(target, 0); // after var target = new Transform3D[collection.Count]; collection.CopyTo(target, 0);
Defensive patterns
Strategy: validation
Validate before calling
var elemType = array.GetType().GetElementType();
if (!typeof(Transform3D).IsAssignableFrom(elemType) && elemType != typeof(object))
throw new ArgumentException("Bad destination array type"); Type guard
bool IsCompatible(Array a) => a != null && (a.GetType().GetElementType() == typeof(Transform3D) || a.GetType().GetElementType() == typeof(object));
Try / catch
try { ((ICollection)collection).CopyTo(arr, 0); } catch (ArgumentException e) when (e.InnerException is InvalidCastException) { /* reallocate correctly-typed array */ } Prevention
- Use Transform3D[] arrays or the generic CopyTo overload
- Verify element type compatibility before reflection-driven copies
- Avoid sharing destination arrays between differently-typed collections
When it happens
Trigger: Calling the non-generic ICollection.CopyTo with an array whose element type cannot hold Transform3D (e.g. new Visual3D[3] or new object[3] only works via boxing, but an unrelated type like new string[3] fails), causing SetValue to throw InvalidCastException.
Common situations: Legacy ICollection-based code passing arrays typed for another collection's element type; refactoring element types between Visual3D/Transform3D collections; reflection-driven copy utilities.
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
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- Cannot pass multidimensional array to the CopyTo method on…
- SR.Collection_BadDestArray
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/31e3ef59a98352bb.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/Transform3DCollection.cs:427
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)