dotnet/wpf · error · ArgumentException
SR.Collection_BadDestArray
Error message
SR.Collection_BadDestArray
What it means
Model3DCollection.CopyTo catches InvalidCastException from Array.SetValue while copying elements and rethrows it as ArgumentException(SR.Collection_BadDestArray, e) with the collection type name. This happens when the destination array's element type cannot accept a Model3D (e.g. a non-Model3D array passed through the non-generic ICollection.CopyTo).
Solutions
- Copy into a Model3D[] or a covariantly compatible array (e.g. Model3D[] to object[] with enough capacity).
- Verify array element type with typeof(Model3D).IsAssignableFrom(array.GetType().GetElementType()) before CopyTo.
- Inspect the inner InvalidCastException in the ArgumentException for the exact type conflict.
Example fix
// before var bad = new Visual3D[collection.Count]; ((ICollection)collection).CopyTo(bad, 0); // InvalidCastException -> Collection_BadDestArray // after var good = new Model3D[collection.Count]; collection.CopyTo(good, 0);
Defensive patterns
Strategy: validation
Validate before calling
var et = array.GetType().GetElementType();
if (et == null || !typeof(Model3D).IsAssignableFrom(et)) throw new ArgumentException("Destination array element type must be assignable from Model3D."); Type guard
bool IsCompatibleDest(Array a) => a?.GetElementType() is Type t && typeof(Model3D).IsAssignableFrom(t);
Try / catch
try { collection.CopyTo(array, index); } catch (ArgumentException ex) when (ex.InnerException is InvalidCastException) { /* use correctly typed array */ } Prevention
- Copy only into Model3D[] (or arrays whose element type derives from Model3D)
- Check GetElementType().IsAssignableFrom before non-generic CopyTo
- Prefer the generic Model3DCollection.CopyTo over ICollection.CopyTo
When it happens
Trigger: Calling the non-generic ICollection.CopyTo with an array of an incompatible element type (e.g. object[] filled with other types is fine, but int[] or Visual[] fails); passing a Model3DCollection's items into a wrongly-typed buffer.
Common situations: Legacy non-generic ICollection-based code paths; reflection or COM interop helpers that treat collections as IList and pass loosely typed arrays.
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.Format(SR.Collection_BadDestArray, "Visual3DCollection")
- Cannot pass multidimensional array to the CopyTo method on…
- SR.Argument_InvalidArrayType
- SR.CannotConvertType
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ebd81c827670bac3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/Model3DCollection.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)