dotnet/wpf · error · ArgumentException
SR.Format(SR.Collection_BadDestArray, this.GetType().Name)
Error message
SR.Format(SR.Collection_BadDestArray, this.GetType().Name)
What it means
VectorCollection.CopyTo writes each element into the destination array with Array.SetValue, which throws InvalidCastException when an element cannot be cast to the array's element type (e.g., copying into a Point[] or string[] instead of Vector[]). CopyTo catches that and rethrows ArgumentException(SR.Format(SR.Collection_BadDestArray, this.GetType().Name)) with the original exception as InnerException, matching BCL collection conventions.
Solutions
- Copy into an array whose element type is exactly Vector (or a compatible base type such as object)
- Check the destination array's element type before calling CopyTo (typeof(Vector).IsAssignableFrom(array.GetType().GetElementType()))
- Read the InnerException of the ArgumentException to confirm the original InvalidCastException during diagnostics
Example fix
// before var dest = new Point[collection.Count]; collection.CopyTo(dest, 0); // after var dest = new Vector[collection.Count]; collection.CopyTo(dest, 0);
Defensive patterns
Strategy: type-guard
Validate before calling
var elemType = dest?.GetType().GetElementType();
if (elemType == null || !typeof(Vector).IsAssignableFrom(elemType))
throw new ArgumentException($"Destination array element type must be Vector (got {elemType?.Name})."); Type guard
static bool IsVectorArray(Array a) => a != null && a.Rank == 1 && typeof(Vector).IsAssignableFrom(a.GetType().GetElementType());
Try / catch
try { collection.CopyTo(dest, index); }
catch (ArgumentException ex) when (ex.InnerException is InvalidCastException)
{ /* dest element type incompatible; allocate Vector[] and retry */ } Prevention
- Declare copy destinations as Vector[] explicitly
- Remember VectorCollection copies Vectors, unlike PointCollection which copies Points
- Inspect InnerException (InvalidCastException) when diagnosing CopyTo ArgumentExceptions
When it happens
Trigger: Calling ((ICollection)vectorCollection).CopyTo(array, index) where array's element type is not assignable from Vector (e.g., a Point[], object[] is fine, double[] is not), so array.SetValue fails with InvalidCastException inside the try block.
Common situations: Assuming VectorCollection copies to Point arrays like some WPF collections do; generic array-conversion helpers with wrong T; refactors where the element type changed between collection and destination buffer.
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
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_ArrayCannotBeMultidimensional
- Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength
- Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength
- Collection_CopyTo_NumberOfElementsExceedsArrayLength
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/88a8171f9ee4f879.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/VectorCollection.cs:382
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)