dotnet/wpf · error · InvalidOperationException

Opening bracket character cannot be the same as closing…

Error message

Opening bracket character cannot be the same as closing bracket character.

What it means

IsValidBracketCharacter (called from Tokenize) throws when the opening and closing bracket characters are identical. The tokenizer needs distinct characters to bracket a delimited region; an identical pair would make tokenization ambiguous and is rejected eagerly with a descriptive message.

Solutions

  1. Change the bracket pair so opening and closing characters differ (e.g. '[' and ']')
  2. Audit the Dictionary<char,char> source for entries where key equals value
  3. Add a pre-check before registering: if (open == close) choose a different closing character

Example fix

// before
var map = new Dictionary<char, char> { ['|'] = '|' };
// after
var map = new Dictionary<char, char> { ['|'] = '|' }; // unsupported
// use an asymmetric pair instead:
var map = new Dictionary<char, char> { ['|'] = ';' };
Defensive patterns

Strategy: validation

Validate before calling

if (open == close) throw new ArgumentException("Opening and closing bracket characters must differ.");

Try / catch

try { tokenize(map); }
catch (InvalidOperationException ex) when (ex.Message.Contains("same as closing")) { /* fix config: reject symmetric pairs */ }

Prevention

When it happens

Trigger: Tokenize runs over an attribute list where a dictionary entry maps a char to the same char as its key, e.g. new Dictionary<char,char>{['|']='|'} — opening == closing triggers this throw.

Common situations: Custom bracket configuration where a developer intends a symmetric delimiter like |...| but the API requires asymmetric pairs; or a config table was built programmatically with key == value by mistake.

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

Appendix: source

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

            {
                foreach (char openingBracket in attributeList.Keys)
                {
                    char closingBracket = attributeList[openingBracket];
                    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());

View on GitHub (pinned to 81131a70a4)