dotnet/wpf · error · ArgumentException

SR.Format(SR.CannotConvertType, typeof(CharacterMetrics)…

Error message

SR.Format(SR.CannotConvertType, typeof(CharacterMetrics), value.GetType())

What it means

CharacterMetricsDictionary values must be CharacterMetrics instances. ConvertValue throws this ArgumentException when the value object is of any other type (and non-null), since it cannot be converted to CharacterMetrics.

Solutions

  1. Pass a CharacterMetrics instance as the value
  2. Construct one: new CharacterMetrics(...) with the advance-width/height/black-box values
  3. Convert or map your source value to CharacterMetrics before calling Add

Example fix

// before
 dictionary.Add(65, "0.1,0.2,0.3,0.4");
// after
 dictionary.Add(65, new CharacterMetrics("0.1,0.2,0.3,0.4"));
Defensive patterns

Strategy: type-guard

Validate before calling

bool IsValidValue(object v) => v is CharacterMetrics;

Type guard

bool IsCharacterMetrics(object v) => v is CharacterMetrics;

Try / catch

try { dict.Add(key, boxedValue); } catch (ArgumentException) { /* value not CharacterMetrics; construct one */ }

Prevention

When it happens

Trigger: Calling Add(object, object) or the non-generic IDictionary members with a value that is not a CharacterMetrics (e.g. a string, double, or custom glyph-metrics object) via an untyped reference.

Common situations: Populating the dictionary from deserialized configuration where values were strings or numbers; using the IDictionary interface with heterogeneous data.

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/814942ed5d2cb1af. Report an issue: GitHub.

Appendix: source

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

            {
                throw new ArgumentException(SR.Format(SR.CannotConvertType, key.GetType(), "int"), nameof(key));
            }

            if (value < 0 || value > FontFamilyMap.LastUnicodeScalar)
                throw new ArgumentException(SR.Format(SR.CodePointOutOfRange, value), nameof(key));

            return value;
        }

        private CharacterMetrics ConvertValue(object value)
        {
            CharacterMetrics metrics = value as CharacterMetrics;
            if (metrics != null)
                return metrics;

            ArgumentNullException.ThrowIfNull(value);

            throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(CharacterMetrics), value.GetType()));
        }

        private struct Enumerator : SC.IDictionaryEnumerator, IEnumerator<KeyValuePair<int, CharacterMetrics>>
        {
            private CharacterMetricsDictionary _dictionary;
            private int _unicodeScalar;
            private CharacterMetrics _value;

            internal Enumerator(CharacterMetricsDictionary dictionary)
            {
                _dictionary = dictionary;
                _unicodeScalar = -1;
                _value = null;
            }

            void IDisposable.Dispose()
            {
            }

View on GitHub (pinned to 81131a70a4)