dotnet/wpf · error · ArgumentException
SR.CannotConvertType
Error message
SR.CannotConvertType
What it means
The explicit ICollection.CopyTo throws ArgumentException when the destination Array's element type cannot accept a System.Collections.DictionaryEntry — i.e. elementType.IsAssignableFrom(typeof(DictionaryEntry)) is false (after the array passed the multidimensional check).
Solutions
- Use DictionaryEntry[] (or object[]) as the destination for the non-generic CopyTo.
- Use the strongly typed CopyTo(KeyValuePair<XmlLanguage,string>[], int) overload instead.
- Project entries with LINQ (dict.Select(kv => new DictionaryEntry(kv.Key, kv.Value)).ToArray()) into the type you need.
Example fix
// before var arr = new string[dict.Count]; ((ICollection)dict).CopyTo(arr, 0); // string not assignable from DictionaryEntry // after var arr = new DictionaryEntry[dict.Count]; ((ICollection)dict).CopyTo(arr, 0);
Defensive patterns
Strategy: type-guard
Validate before calling
var elementType = array.GetType().GetElementType(); if (!elementType.IsAssignableFrom(typeof(DictionaryEntry))) array = new DictionaryEntry[((ICollection)dict).Count]; ((ICollection)dict).CopyTo(array, index);
Type guard
static bool CanHoldDictionaryEntries(Array a) => a.Rank == 1 && a.GetType().GetElementType().IsAssignableFrom(typeof(DictionaryEntry));
Try / catch
try { ((ICollection)dict).CopyTo(array, index); }
catch (ArgumentException ex) when (ex.Message.Contains("CannotConvertType")) { /* switch to DictionaryEntry[] */ } Prevention
- Use DictionaryEntry[] or object[] for non-generic CopyTo.
- Prefer the strongly typed generic overload.
- Project entries via LINQ into the desired element type.
When it happens
Trigger: Calling (ICollection)dict.CopyTo with an array of an incompatible element type, e.g. new string[dict.Count] or new int[dict.Count], routed through the generic Array branch.
Common situations: Copy-pasting copy code that used object[] or DictionaryEntry[] into code with a different array type; interop APIs that hand back typed buffers (string[]); generics that inferred the wrong element type.
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_BadDestArray
- SR.Collection_BadDestArray
- SR.Collection_BadType
- SR.Collection_BadType (double)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/8f0596a539958db5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CultureSpecificStringDictionary.cs:170
SC.DictionaryEntry[] typedArray = array as SC.DictionaryEntry[];
if (typedArray != null)
{
// it's an array of the exact type
foreach (KeyValuePair<XmlLanguage, string> item in _innerDictionary)
{
typedArray[index++] = new SC.DictionaryEntry(item.Key, item.Value);
}
}
else
{
// it's an array of some other type, e.g., object[]; make sure it's one dimensional
if (array.Rank != 1)
throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);
// make sure the element type is compatible
Type elementType = array.GetType().GetElementType();
if (!elementType.IsAssignableFrom(typeof(SC.DictionaryEntry)))
throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(SC.DictionaryEntry), elementType));
foreach (KeyValuePair<XmlLanguage, string> item in _innerDictionary)
{
array.SetValue(new SC.DictionaryEntry(item.Key, item.Value), index++);
}
}
}
#endregion
#region IDictionary members
/// <summary>
/// Adds a language and associated string to the collection.
/// </summary>
public void Add(XmlLanguage key, string value)
{
_innerDictionary.Add(key, ValidateValue(value));View on GitHub (pinned to 81131a70a4)