dotnet/wpf · error · ArgumentException

SR.Format(SR.CannotConvertStringToType, s, "int")

Error message

SR.Format(SR.CannotConvertStringToType, s, "int")

What it means

CharacterMetricsDictionary keys must be Unicode code points (int) or hex strings parseable to one. ConvertKey throws this ArgumentException when a string key cannot be fully parsed as a hexadecimal number by FontFamilyMap.ParseHexNumber (e.g. because i < s.Length, meaning trailing garbage remains after the parsed prefix).

Solutions

  1. Use bare hexadecimal string keys without prefixes or suffixes, e.g. "41" instead of "0x41"
  2. Pass the key as an int (the Unicode scalar value) instead of a string
  3. Trim the string and verify it parses with int.Parse(s, NumberStyles.HexNumber) before calling Add

Example fix

// before
 dictionary.Add("0x41", new CharacterMetrics());
// after
 dictionary.Add("41", new CharacterMetrics()); // or dictionary.Add(0x41, ...)
Defensive patterns

Strategy: validation

Validate before calling

bool IsValidHexKey(string s) => !string.IsNullOrEmpty(s) && int.TryParse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int v) && v >= 0 && v <= 0x10FFFF;

Type guard

bool IsHexKey(object key) => key is int || (key is string s && int.TryParse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out _));

Try / catch

try { dict.Add(key, value); } catch (ArgumentException ex) when (ex.ParamName == "key") { /* log/fix key format */ }

Prevention

When it happens

Trigger: Passing a string key to Add(object, object), the IDictionary members, or the indexer whose text is not a complete hex number, such as "41x", "0x41", or "" — the parse either fails or stops before the end of the string.

Common situations: Populating the dictionary from config/XML where keys were authored as decimal ("65") or C-style hex ("0x41") instead of the expected bare hex form ("41"); copy-pasted keys with whitespace or trailing characters.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

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

            foreach (KeyValuePair<int, CharacterMetrics> pair in this)
            {
                result[i++] = pair.Value;
            }
            return result;
        }

        internal static int ConvertKey(object key)
        {
            ArgumentNullException.ThrowIfNull(key);

            int value;

            string s = key as string;
            if (s != null)
            {
                int i = 0;
                if (!FontFamilyMap.ParseHexNumber(s, ref i, out value) || i < s.Length)
                    throw new ArgumentException(SR.Format(SR.CannotConvertStringToType, s, "int"), nameof(key));
            }
            else if (key is int)
            {
                value = (int)key;
            }
            else
            {
                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)
        {

View on GitHub (pinned to 81131a70a4)