dotnet/wpf · error · ArgumentException
SR.Format(SR.CannotConvertType, typeof(FamilyTypeface)…
Error message
SR.Format(SR.CannotConvertType, typeof(FamilyTypeface), elementType)
What it means
The explicit ICollection.CopyTo(Array, int) of FamilyMapCollection requires the destination element type to accept FontFamilyMap elements; if elementType.IsAssignableFrom(typeof(FamilyTypeface)) fails (note: the collection's element type check used in this code path), it throws ArgumentException with CannotConvertType naming the source and target types.
Solutions
- Use a FontFamilyMap[] (or a base type/interface array the element is assignable to) as the destination.
- Check array.GetType().GetElementType().IsAssignableFrom(typeof(FontFamilyMap)) before copying.
- Use ToArray()/Cast<T>() instead of the non-generic ICollection path when types are uncertain.
Example fix
// before Array dest = Array.CreateInstance(typeof(int), familyMaps.Count); ((ICollection)familyMaps).CopyTo(dest, 0); // after var dest = new FontFamilyMap[familyMaps.Count]; ((ICollection)familyMaps).CopyTo(dest, 0);
Defensive patterns
Strategy: type-guard
Validate before calling
bool ok = array.GetType().GetElementType().IsAssignableFrom(typeof(FontFamilyMap));
Type guard
static bool CanHoldFamilyMaps(Array a) => a.GetType().GetElementType()?.IsAssignableFrom(typeof(FontFamilyMap)) == true;
Try / catch
try { ((ICollection)col).CopyTo(array, index); }
catch (ArgumentException) { /* use correctly typed array */ } Prevention
- Use FontFamilyMap[] destinations rather than Array.CreateInstance buffers
- Keep generic overloads over non-generic ICollection paths
When it happens
Trigger: Calling ((ICollection)familyMapCollection).CopyTo with a non-assignable array type such as object[]-typed Array instances created via Array.CreateInstance, or arrays of an unrelated type like FontFamily[] or int[].
Common situations: Reflection-based serialization writing into Array.CreateInstance(typeof(...), n) buffers, or mismatched generic/legacy collection bridges.
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_BadType (Drawing)
- SR.Collection_BadType
- SR.Collection_CopyTo_ArrayCannotBeMultidimensional
- SR.CompositeFont_TooManyFamilyMaps
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/c219071ae426f5bd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs:95
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array"));
if (_count > array.Length - index)
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, "array"));
if (_count != 0)
Array.Copy(_items, 0, array, index, _count);
}
void SC.ICollection.CopyTo(Array array, int index)
{
ArgumentNullException.ThrowIfNull(array);
if (array.Rank != 1)
throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);
Type elementType = array.GetType().GetElementType();
if (!elementType.IsAssignableFrom(typeof(FamilyTypeface)))
throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(FamilyTypeface), elementType));
if (index >= array.Length)
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array"));
if (_count > array.Length - index)
throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, "array"));
if (_count != 0)
Array.Copy(_items, 0, array, index, _count);
}
bool SC.ICollection.IsSynchronized
{
get { return false; }
}
object SC.ICollection.SyncRoot
{View on GitHub (pinned to 81131a70a4)