dotnet/wpf · error · TypeNameParserException
SR.InvalidCharInTypeName
Error message
SR.InvalidCharInTypeName
What it means
GenericTypeNameParser.ThrowOnBadInput throws TypeNameParserException (SR.InvalidCharInTypeName) when the scanner encounters a character that cannot appear in a generic type name, reporting the offending character and the full input text. It is the error exit of the parser loop reached from ParseName, ParseList, P_XamlTypeName, P_SimpleTypeName, and P_TypeParameters.
Solutions
- Fix the type name string so it is syntactically valid (balanced brackets, valid identifier characters)
- Validate/normalize the type string before passing it to XAML type resolution
- Use Type.GetType or the fully qualified type reference instead of hand-built names
Example fix
// before
var type = new XamlTypeName("ns", "List[Int32"); // unclosed subscript
// after
var type = new XamlTypeName("ns", "List[Int32]"); Defensive patterns
Strategy: try-catch
Validate before calling
// validate the type name string before parsing
if (string.IsNullOrWhiteSpace(typeName) || typeName.Count(c => c == '[') != typeName.Count(c => c == ']'))
throw new FormatException($"Malformed type name: {typeName}"); Type guard
static bool LooksLikeTypeName(string s) => !string.IsNullOrWhiteSpace(s) && s.Count(c => c=='[') == s.Count(c => c==']');
Try / catch
try { var name = ParseGenericTypeName(input); }
catch (TypeNameParserException ex) { /* invalid char in type name: ex.Message includes char and input */ } Prevention
- Ensure brackets/subscripts in generic type names are balanced
- Escape or avoid invalid characters in type reference strings
- Prefer fully qualified type names over hand-built ones
When it happens
Trigger: Parsing a type name string (e.g. from x:Type markup extension or XamlType resolution) containing an invalid character — stray punctuation, unclosed brackets, spaces in wrong positions, or truncated generic syntax.
Common situations: Hand-written type name strings in XAML x:Type references; dynamically built type names with malformed generic arity syntax (e.g. "List(Of T" ); typos in assembly-qualified type references.
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
- member
- SR.BamlReaderNoOwnerType
- SR.CantGetWriteonlyProperty
- SR.CantSetReadonlyProperty
- SR.CollectionCannotContainNulls
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/158151104887f1c3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Parser/GenericTypeNameParser.cs:252
// Subscript ::= ‘[’ ‘,’* ‘]’
//
private void P_RepeatingSubscript()
{
// caller checks this.
Debug.Assert(_scanner.Token == GenericTypeNameScannerToken.SUBSCRIPT);
do
{
Callout_Subscript(_scanner.MultiCharTokenText);
_scanner.Read();
}
while (_scanner.Token == GenericTypeNameScannerToken.SUBSCRIPT);
}
private void ThrowOnBadInput()
{
throw new TypeNameParserException(SR.Format(SR.InvalidCharInTypeName, _scanner.ErrorCurrentChar, _inputText));
}
private void StartStack()
{
_stack = new Stack<TypeNameFrame>();
TypeNameFrame frame;
frame = new TypeNameFrame();
_stack.Push(frame);
}
private void Callout_FoundName(string prefix, string name)
{
TypeNameFrame frame = new TypeNameFrame
{
Name = name
};
string ns = _prefixResolver(prefix);
frame.Namespace = ns ?? throw new TypeNameParserException(SR.Format(SR.PrefixNotFound, prefix));View on GitHub (pinned to 81131a70a4)