dotnet/wpf · error · InvalidOperationException

Bracket characters cannot be alpha-numeric or whitespace.

Error message

Bracket characters cannot be alpha-numeric or whitespace.

What it means

IsValidBracketCharacter throws when either the opening or closing bracket character is a letter, digit, or whitespace. Bracket characters are markup delimiters and must be unambiguous symbols; alphanumeric or whitespace characters would collide with normal attribute text during tokenization.

Solutions

  1. Replace any alphanumeric character in the pair with a punctuation symbol (e.g. '[', ']', '<', '>', '|')
  2. Strip whitespace from configured delimiter characters before registering
  3. Validate the pair with char.IsLetterOrDigit/IsWhiteSpace checks before calling into the parser

Example fix

// before
parser.AddBracketCharacters('a', 'b');
// after
parser.AddBracketCharacters('<', '>');
Defensive patterns

Strategy: validation

Validate before calling

bool IsSymbolic(char c) => !char.IsLetterOrDigit(c) && !char.IsWhiteSpace(c);

Try / catch

try { tokenize(map); }
catch (InvalidOperationException ex) when (ex.Message.Contains("alpha-numeric")) { /* sanitize map: drop letter/digit/space entries */ }

Prevention

When it happens

Trigger: Tokenize processes a bracket dictionary containing entries like ['a']='b', ['1']='2', or whitespace keys/values; char.IsLetterOrDigit or char.IsWhiteSpace returns true for either character.

Common situations: Attempted use of word-like or space-delimited custom escapes in XAML attributes, e.g. trying to use a space or a letter as the custom bracket delimiter.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/Xaml/Parser/SpecialBracketCharacters.cs:80

                    string errorMessage = string.Empty;
                    if (IsValidBracketCharacter(openingBracket, closingBracket))
                    {
                        _startCharactersStringBuilder.Append(openingBracket);
                        _endCharactersStringBuilder.Append(closingBracket);
                    }
                }
            }
        }

        private bool IsValidBracketCharacter(char openingBracket, char closingBracket)
        {
            if (openingBracket == closingBracket)
            {
                throw new InvalidOperationException("Opening bracket character cannot be the same as closing bracket character.");
            }
            else if (char.IsLetterOrDigit(openingBracket) || char.IsLetterOrDigit(closingBracket) || char.IsWhiteSpace(openingBracket) || char.IsWhiteSpace(closingBracket))
            {
                throw new InvalidOperationException("Bracket characters cannot be alpha-numeric or whitespace.");
            }
            else if (_restrictedCharSet.Contains(openingBracket) || _restrictedCharSet.Contains(closingBracket))
            {
                throw new InvalidOperationException("Bracket characters cannot be one of the following: '=' , ',', '\'', '\"', '{ ', ' }', '\\'");
            }
            else
            {
                return true;
            }
        }

        internal bool IsSpecialCharacter(char ch)
        {
            return _startChars.Contains(ch.ToString()) || _endChars.Contains(ch.ToString());
        }

        internal bool StartsEscapeSequence(char ch)
        {

View on GitHub (pinned to 81131a70a4)