{"record":{"id":"44782090b5d8c8b2","repo":"dotnet/wpf","slug":"throw-new-invalidoperationexception","errorCode":null,"errorMessage":"throw new InvalidOperationException();","messagePattern":"throw new InvalidOperationException\\(\\);","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/Xaml/Parser/SpecialBracketCharacters.cs","lineNumber":51,"sourceCode":"        internal SpecialBracketCharacters(IReadOnlyDictionary<char, char> attributeList)\n        {\n            BeginInit();\n            if (attributeList is not null && attributeList.Count > 0)\n            {\n                Tokenize(attributeList);\n            }\n        }\n\n        internal void AddBracketCharacters(char openingBracket, char closingBracket)\n        {\n            if (_initializing)\n            {\n                _startCharactersStringBuilder.Append(openingBracket);\n                _endCharactersStringBuilder.Append(closingBracket);\n            }\n            else\n            {\n                throw new InvalidOperationException();\n            }\n        }\n\n        private void Tokenize(IReadOnlyDictionary<char, char> attributeList)\n        {\n            if (_initializing)\n            {\n                foreach (char openingBracket in attributeList.Keys)\n                {\n                    char closingBracket = attributeList[openingBracket];\n                    string errorMessage = string.Empty;\n                    if (IsValidBracketCharacter(openingBracket, closingBracket))\n                    {\n                        _startCharactersStringBuilder.Append(openingBracket);\n                        _endCharactersStringBuilder.Append(closingBracket);\n                    }\n                }\n            }","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/Xaml/Parser/SpecialBracketCharacters.cs#L33-L69","documentation":"AddBracketCharacters throws a bare InvalidOperationException when a caller registers a bracket pair whose characters fail the bracket constraints. The class tokenizes custom opening/closing bracket characters for XAML-style markup extension parsing, and it refuses to append a pair that is not a valid bracket character. The message is empty, so the real cause must be inferred from the character values passed in.","triggerScenarios":"Calling AddBracketCharacters(openingBracket, closingBracket) with a pair that violates the rules enforced by IsValidBracketCharacter: identical open/close characters, alphanumeric or whitespace characters, or a character in the restricted set (= , ' \" { } \\). Note Tokenize/initialization paths route through the same validation.","commonSituations":"Custom markup extension configuration in WPF XAML parsing where an escape/bracket override uses e.g. '[' and ']' is fine, but a developer passes '(' and '(' (same char), or uses a letter like 'q' as a delimiter, or reuses '{' as an opening bracket while it is already reserved.","solutions":["Inspect the openingBracket and closingBracket arguments; ensure they are different, non-alphanumeric, non-whitespace characters","Exclude restricted characters: = , ' \" { } \\ — pick another symbol pair such as [ ] or < >","Verify initialization order: brackets can only be added while _initializing is true; adding after tokenization started invalidates state","Wrap the AddBracketCharacters call in a try-catch and log the character codes for diagnostics"],"exampleFix":"// before\nparser.AddBracketCharacters('(', '(');\n// after\nparser.AddBracketCharacters('(', ')');","handlingStrategy":"validation","validationCode":"bool IsValidPair(char open, char close) =>\n    open != close &&\n    !char.IsLetterOrDigit(open) && !char.IsLetterOrDigit(close) &&\n    !char.IsWhiteSpace(open) && !char.IsWhiteSpace(close) &&\n    !\"=,'\\\"{}\\\\\".Contains(open) && !\"=,'\\\"{}\\\\\".Contains(close);","typeGuard":null,"tryCatchPattern":"try { parser.AddBracketCharacters(open, close); }\ncatch (InvalidOperationException ex) { log.Warn($\"Invalid bracket pair {(int)open}/{(int)close}: {ex.Message}\"); }","preventionTips":["Validate the pair with char checks before registering","Never use restricted characters = , ' \" { } \\","Keep bracket configuration static and tested, not user-supplied"],"tags":["wpf","xaml","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}