dotnet/wpf · error · ArgumentException

SR.Format(SR.CannotConvertType, typeof(SC.DictionaryEntry)…

Error message

SR.Format(SR.CannotConvertType, typeof(SC.DictionaryEntry), elementType)

What it means

When CopyTo's destination is an Array of some other element type (e.g. object[] is fine, but string[] is not), the library checks elementType.IsAssignableFrom(typeof(DictionaryEntry)). If the element type cannot hold a DictionaryEntry struct, an ArgumentException (CannotConvertType) is thrown naming both types.

Solutions

  1. Use a DictionaryEntry[] array (fast path) or an object[] array.
  2. Otherwise ensure the array's element type is DictionaryEntry or a base type/interface it converts to (object, ValueType, IStructuralEquatable...).
  3. Manually enumerate the dictionary and project entries into your target type instead of using CopyTo.

Example fix

// before
var arr = new string[dict.Count];
((ICollection)dict).CopyTo(arr, 0);
// after
var arr = new DictionaryEntry[dict.Count];
((ICollection)dict).CopyTo(arr, 0);
Defensive patterns

Strategy: validation

Validate before calling

Type elementType = array.GetType().GetElementType();
if (!elementType.IsAssignableFrom(typeof(DictionaryEntry)))
    array = new DictionaryEntry[dict.Count];

Try / catch

try { ((ICollection)dict).CopyTo(array, index); }
catch (ArgumentException) { /* use DictionaryEntry[] or object[] */ }

Prevention

When it happens

Trigger: Calling ((ICollection)CharacterMetricsDictionary).CopyTo(array, 0) where array is a 1D array of an incompatible element type, such as string[], int[], or a custom unrelated type.

Common situations: Refactoring that changed the scratch array type; copy/paste of CopyTo calls between collections with different element types; arrays created generically with wrong T.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/74fa1b91e579fe3e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs:201

            SC.DictionaryEntry[] typedArray = array as SC.DictionaryEntry[];
            if (typedArray != null)
            {
                // it's an array of the exact type
                foreach (KeyValuePair<int, CharacterMetrics> item in this)
                {
                    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<int, CharacterMetrics> item in this)
                {
                    array.SetValue(new SC.DictionaryEntry(item.Key, item.Value), index++);
                }
            }
        }

        #endregion

        #region IDictionary members

        /// <summary>
        /// Adds a character code and associated CharacterMetrics to the collection.
        /// </summary>
        public void Add(int key, CharacterMetrics value)
        {
            SetValue(key, value, failIfExists: true);

View on GitHub (pinned to 81131a70a4)