dotnet/wpf · error · TypeNameParserException

SR.InvalidTypeString

Error message

SR.InvalidTypeString

What it means

CollectNameFromStack validates that the parsed type name left exactly one frame on the parser stack. If input like 'ns:Type' parsed into multiple (or zero) frames — e.g. unbalanced parentheses or trailing tokens — the structure is not a single valid type name, so InvalidTypeString is thrown with the raw input text.

Solutions

  1. Verify the type string is a single well-formed name like 'prefix:TypeName' or '(prefix:T1,prefix:T2)'
  2. Check for unbalanced parentheses or stray commas in the string
  3. Fix code that joins multiple type names when only one is accepted

Example fix

// before
var t = XamlTypeName.Parse("{ns:Foo,ns:Bar}", resolver, out err);
// after
var t = XamlTypeName.Parse("ns:Foo", resolver, out err);
Defensive patterns

Strategy: validation

Validate before calling

bool IsSingleTypeName(string s) =>
    !string.IsNullOrWhiteSpace(s) && s.Count(c => c == '(') == s.Count(c => c == ')') && !s.Trim().EndsWith(",");

Try / catch

try { var t = XamlTypeName.Parse(input, resolver, out var err); }
catch (TypeNameParserException) { /* log input text; fix syntax */ }

Prevention

When it happens

Trigger: XamlTypeName.Parse called with syntactically broken input: unbalanced generic braces 'ns:T1,ns:T2)', stray commas, or multiple comma-separated type names where a single type was expected.

Common situations: Hand-writing markup-extension arguments, storing type names in config strings, generically constructing type names in code with wrong join 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/547a53e191d8b43b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Parser/GenericTypeNameParser.cs:298

            if (frame.TypeArgs is null)
            {
                frame.AllocateTypeArgs();
            }

            frame.TypeArgs.Add(typeName);
        }

        private void Callout_Subscript(string subscript)
        {
            TypeNameFrame frame = _stack.Peek();
            frame.Name += subscript;
        }

        private XamlTypeName CollectNameFromStack()
        {
            if (_stack.Count != 1)
            {
                throw new TypeNameParserException(SR.Format(SR.InvalidTypeString, _inputText));
            }

            TypeNameFrame frame = _stack.Peek();
            if (frame.TypeArgs.Count != 1)
            {
                throw new TypeNameParserException(SR.Format(SR.InvalidTypeString, _inputText));
            }

            XamlTypeName xamlTypeName = frame.TypeArgs[0];
            return xamlTypeName;
        }

        private IList<XamlTypeName> CollectNameListFromStack()
        {
            if (_stack.Count != 1)
            {
                throw new TypeNameParserException(SR.Format(SR.InvalidTypeString, _inputText));
            }

View on GitHub (pinned to 81131a70a4)