{"record":{"id":"7b9113bd716796b3","repo":"dotnet/wpf","slug":"sr-wrongselectiontype","errorCode":null,"errorMessage":"SR.WrongSelectionType","messagePattern":"SR\\.WrongSelectionType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionHelper.cs","lineNumber":228,"sourceCode":"\n        /// <summary>\n        ///  Gets the anchor point for the selection\n        /// </summary>\n        /// <param name=\"selection\">the anchor to examine</param>\n        /// <returns>\n        /// The anchor point of the selection; \n        /// If there is no valid AnchorPoint returns Point(Double.NaN, Double.NaN). \n        /// </returns>\n        /// <exception cref=\"ArgumentNullException\">anchor is null</exception>\n        /// <exception cref=\"ArgumentException\">anchor is of wrong type</exception>\n        public static Point GetAnchorPoint(Object selection)\n        {\n            ArgumentNullException.ThrowIfNull(selection);\n\n            TextAnchor anchor = selection as TextAnchor;\n\n            if (anchor == null)\n                throw new ArgumentException(SR.WrongSelectionType, nameof(selection));\n\n            return GetAnchorPointForPointer(anchor.Start.CreatePointer(LogicalDirection.Forward));\n        }\n\n        /// <summary>\n        ///  Gets the anchor point for the text pointer\n        /// </summary>\n        /// <param name=\"pointer\">the pointer to examine</param>\n        /// <returns>\n        /// The anchor point of the text pointer\n        /// </returns>\n        /// <exception cref=\"ArgumentNullException\">pointer is null</exception>\n        public static Point GetAnchorPointForPointer(ITextPointer pointer)\n        {\n            ArgumentNullException.ThrowIfNull(pointer);\n\n            Rect rect = GetAnchorRectangle(pointer);\n","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionHelper.cs#L210-L246","documentation":"TextSelectionHelper.GetAnchorPoint computes the anchor (start) point of an annotation selection. It only understands TextAnchor objects; passing any other selection type throws this ArgumentException.","triggerScenarios":"Calling TextSelectionHelper.GetAnchorPoint(selection) with something that is not a TextAnchor — e.g. a TextRange, TextSelection, or arbitrary object.","commonSituations":"Retrieving an anchor from a store/dictionary typed as object and forgetting its concrete type; passing the raw editor selection instead of the framework-produced TextAnchor.","solutions":["Pass a TextAnchor instance; cast the selection before calling.","If you have a TextRange, resolve it through TextSelectionProcessor to obtain a TextAnchor first.","Guard with a type check and skip GetAnchorPoint for non-TextAnchor selections.","Catch ArgumentException around GetAnchorPoint and return a default/null anchor point."],"exampleFix":"// before\nvar point = TextSelectionHelper.GetAnchorPoint(mySelection);\n// after\nvar point = mySelection is TextAnchor anchor\n    ? TextSelectionHelper.GetAnchorPoint(anchor)\n    : (Point?)null;","handlingStrategy":"type-guard","validationCode":"if (!(selection is TextAnchor)) throw new InvalidOperationException(\"GetAnchorPoint requires TextAnchor\");","typeGuard":"static bool IsTextAnchor(object s) => s is TextAnchor;","tryCatchPattern":"try { var p = TextSelectionHelper.GetAnchorPoint(selection); }\ncatch (ArgumentException ex) when (ex.ParamName == nameof(selection)) { p = null; }","preventionTips":["Only call GetAnchorPoint on anchors produced by TextSelectionProcessor.","Narrow to TextAnchor at the call site with pattern matching.","Keep anchor objects out of generic object containers."],"tags":["wpf","annotations","argumentexception","type-mismatch"],"backgroundTag":"type-mismatch","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"}