dotnet/wpf · error · ArgumentException

SR.CharacterMetrics_TooManyFields

Error message

SR.CharacterMetrics_TooManyFields

What it means

CharacterMetrics.ParseMetrics parses a comma-separated metrics string (e.g. from a FontFamily 'DeviceFontName' style definition). The string may contain at most NumFields (= 8: LeftSideBearing, RightSideBearing, TopSideBearing, BottomSideBearing, Width, Height, BlackBoxWidth, BlackBoxHeight) comma-separated fields. This ArgumentException is thrown when the string has more fields than that.

Solutions

  1. Remove extra comma-separated fields so the string has at most 8 values.
  2. Trim trailing/stray commas from the metrics string before constructing CharacterMetrics.
  3. Split the string on ',' and validate the count is between NumRequiredFields (4) and 8 before constructing.

Example fix

// before
new CharacterMetrics("0.1,0.2,0.3,0.4,1.0,2.0,1.2,2.2,0.5");
// after
new CharacterMetrics("0.1,0.2,0.3,0.4,1.0,2.0,1.2,2.2");
Defensive patterns

Strategy: validation

Validate before calling

string[] fields = metricsString.Split(',');
if (fields.Length > 8)
    throw new FormatException($"CharacterMetrics string must have at most 8 fields, got {fields.Length}.");

Prevention

When it happens

Trigger: Calling the CharacterMetrics(string metricsString) constructor with a string containing 9 or more comma-separated values.

Common situations: Hand-edited or machine-generated font metrics strings that append trailing fields or accidentally include extra commas; pasting metrics from a different format with more columns.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetrics.cs:180

                        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;
                }
                else
                {
                    // No more fields; check if we have all required fields.
                    if (fieldIndex < NumRequiredFields)
                    {
                        throw new ArgumentException(SR.CharacterMetrics_MissingRequiredField);
                    }

                    break;
                }
            }

            return metrics;
        }

View on GitHub (pinned to 81131a70a4)