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
CopyTo catches the InvalidCastException raised while copying elements into the destination array and rethrows ArgumentException with SR.Collection_BadDestArray, wrapping the original exception. This means the destination array's element type cannot hold a TextDecoration.
Solutions
- Allocate the destination array as TextDecoration[] or object[]
- Verify array.GetType().GetElementType().IsAssignableFrom(typeof(TextDecoration)) before CopyTo
- Use Cast<TextDecoration>().ToArray() (LINQ) instead of CopyTo for flexible conversion
Example fix
// before var arr = new string[collection.Count]; ((ICollection)collection).CopyTo(arr, 0); // after var arr = new TextDecoration[collection.Count]; collection.CopyTo(arr, 0);
Defensive patterns
Strategy: type-guard
Validate before calling
if (array == null) throw new ArgumentNullException(nameof(array));
var elemType = array.GetType().GetElementType();
if (elemType == null || !elemType.IsAssignableFrom(typeof(TextDecoration))) throw new ArgumentException($"Array element type {elemType} cannot hold TextDecoration"); Type guard
bool IsCompatibleDestArray(Array a) => a is TextDecoration[] || a is object[] || (a != null && a.GetType().GetElementType()?.IsAssignableFrom(typeof(TextDecoration)) == true);
Prevention
- Prefer strongly typed TextDecoration[] destinations
- Avoid ICollection.CopyTo with arbitrary array types
- Use LINQ ToArray/Cast for conversion instead of CopyTo
When it happens
Trigger: Calling ICollection.CopyTo with an array typed to something incompatible, e.g. string[] or non-covariant element types, so Array.SetValue throws InvalidCastException.
Common situations: Copying into object[] is fine, but copying into string[]/int[] fails; reflection-based or serialization code that allocates arrays from the wrong element type; IEnumerable.CopyTo generic helpers.
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.Argument_InvalidArrayType
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/c7694728c4504f4d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs:408
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)