dotnet/wpf · error · ArgumentException
SR.CannotConvertStringToType
Error message
SR.CannotConvertStringToType
What it means
CharacterMetrics.ParseMetrics splits a semicolon-separated metrics string into fields and parses each as an invariant-culture double. When double.TryParse fails for a required field, it throws ArgumentException with SR.CannotConvertStringToType naming the field and target type 'double'.
Solutions
- Correct the field to be an invariant-culture double (use '.' as decimal separator, optional leading sign).
- Re-export the metrics file with InvariantCulture formatting.
- Pre-validate each field with double.TryParse(s, NumberStyles.AllowDecimalPoint | AllowLeadingSign, CultureInfo.InvariantCulture, out _) before construction.
Example fix
// before
new CharacterMetrics("0,1,5;0,75;0;0;0;0;0"); // '1,5' not parseable invariantly
// after
new CharacterMetrics("0,1.5;0,75;0;0;0;0;0"); Defensive patterns
Strategy: validation
Validate before calling
static bool IsInvariantDouble(string s) =>
double.TryParse(s, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS, out _); Try / catch
try { var m = new CharacterMetrics(str); }
catch (ArgumentException ex) when (ex.Message.Contains("double")) { Log($"Bad numeric field: {ex.Message}"); m = null; } Prevention
- Always write metric files using invariant culture ('.' decimal separator, no thousands separators).
- Pre-parse and validate each field with double.TryParse(InvariantCulture) before constructing.
- Strip whitespace/units from numeric fields at import.
When it happens
Trigger: A metrics string field containing non-numeric text, wrong decimal separator (e.g. ',' instead of '.'), or extra characters in a required field position.
Common situations: Locale-edited font metric files (comma decimal separators); truncated or concatenated fields shifting numeric values; whitespace or units ('px') inside numeric fields.
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
- SR.CharacterMetrics_MissingRequiredField
- errMsg (dynamic: message of caught…
- FileFormatException(new Uri(fileName…
- MappingParseError(_scanner.Start, MappingScanner.Ident…
- SR.CharacterMetrics_NegativeHorizontalAdvance
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/74db471d6f8f3b9c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetrics.cs:165
++j;
// Let k be end-of-field without trailing whitespace.
int k = j;
while (k > i && s[k - 1] == ' ')
--k;
if (k > i)
{
// Non-empty field; convert it to double.
ReadOnlySpan<char> field = s.AsSpan(i, k - i);
if (!double.TryParse(
field,
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS,
out metrics[fieldIndex]
))
{
throw new ArgumentException(SR.Format(SR.CannotConvertStringToType, field.ToString(), "double"));
}
}
else if (fieldIndex < NumRequiredFields)
{
// Empty field; make sure it's an optional one.
throw new ArgumentException(SR.CharacterMetrics_MissingRequiredField);
}
++fieldIndex;
if (j < s.Length)
{
// There's a comma so check if we've exceeded the number of fields.
if (fieldIndex == NumFields)
throw new ArgumentException(SR.CharacterMetrics_TooManyFields);
// Initialize character index for next iteration.
i = j + 1;View on GitHub (pinned to 81131a70a4)