{"record":{"id":"510dc454c67689c9","repo":"dotnet/wpf","slug":"sr-wrongselectiontype-selection-type-selection-gettype","errorCode":null,"errorMessage":"SR.WrongSelectionType (selection: type={selection.GetType()})","messagePattern":"SR\\.WrongSelectionType \\(selection: type=(.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/FixedTextSelectionProcessor.cs","lineNumber":449,"sourceCode":"            ITextPointer start = null;\n            ITextRange textRange = selection as ITextRange;\n\n            if (textRange != null)\n            {\n                start = textRange.Start;\n                textSegments = textRange.TextSegments;\n            }\n            else\n            {\n                TextAnchor anchor = selection as TextAnchor;\n                if (anchor != null)\n                {\n                    start = anchor.Start;\n                    textSegments = anchor.TextSegments;\n                }\n                else\n                {\n                    throw new ArgumentException(SR.WrongSelectionType, $\"selection: type={selection.GetType()}\");\n                }\n            }\n\n            if (!(start.TextContainer is FixedTextContainer ||\n                start.TextContainer is DocumentSequenceTextContainer))\n                throw new ArgumentException(SR.WrongSelectionType, $\"selection: type={selection.GetType()}\");\n\n            return textSegments;\n        }\n\n        /// <summary>\n        /// Checks if the selection object satisfies the requirements\n        /// for this processor\n        /// </summary>\n        /// <param name=\"selection\">selection</param>\n        /// <returns>ITextRange interface, implemented by the object</returns>\n        private TextAnchor CheckAnchor(object selection)\n        {","sourceCodeStart":431,"sourceCodeEnd":467,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/FixedTextSelectionProcessor.cs#L431-L467","documentation":"CheckSelection validates that the selection object handed to the processor is a TextAnchor or TextSelection whose start TextContainer is a FixedTextContainer or DocumentSequenceTextContainer. Otherwise it throws ArgumentException(SR.WrongSelectionType) with the actual runtime type. Called from GenerateLocatorParts and textSegments.","triggerScenarios":"Calling GenerateLocatorParts (or the textSegments helper) with a selection that is neither TextAnchor nor TextSelection (first throw), or whose Start.TextContainer is not fixed/sequence content (second throw at line 449 is the container check).","commonSituations":"Passing a TextSelection from a FlowDocumentReader to a fixed-document annotation service; passing arbitrary objects (e.g. a TextPointer) as 'selection'; mixing flow and fixed anchoring processors.","solutions":["Only pass selections created from DocumentPageView/FixedPage content (FixedTextContainer).","Use the TextSelectionProcessor subclass that matches the content type (Fixed vs tree).","Check selection is ITextSelection/TextAnchor and its TextContainer type before calling."],"exampleFix":"// before\nprocessor.GenerateLocatorParts(unknownSelection); // may be a flow selection\n// after\nif (sel is TextSelection ts && (ts.Start.TextContainer is FixedTextContainer || ts.Start.TextContainer is DocumentSequenceTextContainer))\n    processor.GenerateLocatorParts(ts);","handlingStrategy":"type-guard","validationCode":"if (!(selection is TextSelection || selection is TextAnchor)) throw new InvalidOperationException(\"selection must be TextSelection or TextAnchor\");","typeGuard":"bool IsFixedSelection(object s) => s is TextSelection ts && (ts.Start.TextContainer is FixedTextContainer || ts.Start.TextContainer is DocumentSequenceTextContainer);","tryCatchPattern":"try { processor.GenerateLocatorParts(selection); } catch (ArgumentException ex) { /* wrong selection type — re-derive from page view */ }","preventionTips":["Only anchor selections made in fixed-document viewers","Check TextContainer type before calling the processor","Use the matching SelectionProcessor for the content type"],"tags":["wpf","annotations","selection"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}