{"record":{"id":"fbac92859939dded","repo":"dotnet/wpf","slug":"sr-wrongselectiontype-anchor1-type-anchor1-gettype","errorCode":null,"errorMessage":"SR.WrongSelectionType (anchor1: type = {anchor1.GetType()})","messagePattern":"SR\\.WrongSelectionType \\(anchor1: type = (.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionHelper.cs","lineNumber":72,"sourceCode":"        #region Public Methods\n\n        /// <summary>\n        ///     Merges the two anchors into one, if possible.\n        /// </summary>\n        /// <param name=\"anchor1\">anchor to merge </param>\n        /// <param name=\"anchor2\">other anchor to merge </param>\n        /// <param name=\"newAnchor\">new anchor that contains the data from both \n        /// anchor1 and anchor2</param>\n        /// <returns>true if the anchors were merged, false otherwise \n        /// </returns>\n        /// <exception cref=\"ArgumentNullException\">anchor1 or anchor2 are null</exception>\n        public static bool MergeSelections(Object anchor1, Object anchor2, out Object newAnchor)\n        {\n            TextAnchor firstAnchor = anchor1 as TextAnchor;\n            TextAnchor secondAnchor = anchor2 as TextAnchor;\n\n            if ((anchor1 != null) && (firstAnchor == null))\n                throw new ArgumentException(SR.WrongSelectionType, $\"anchor1: type = {anchor1.GetType()}\");\n\n            if ((anchor2 != null) && (secondAnchor == null))\n                throw new ArgumentException(SR.WrongSelectionType, $\"Anchor2: type = {anchor2.GetType()}\");\n\n            if (firstAnchor == null)\n            {\n                newAnchor = secondAnchor;\n                return newAnchor != null;\n            }\n\n            if (secondAnchor == null)\n            {\n                newAnchor = firstAnchor;\n                return newAnchor != null;\n            }\n\n            newAnchor = TextAnchor.ExclusiveUnion(firstAnchor, secondAnchor);\n","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionHelper.cs#L54-L90","documentation":"TextSelectionHelper.MergeSelections merges two annotation anchors into one. The anchor1 parameter must be either null or a TextAnchor; any other type throws this ArgumentException because only TextAnchor instances can be merged.","triggerScenarios":"Calling TextSelectionHelper.MergeSelections with an anchor1 that is non-null but not a TextAnchor — e.g. passing a TextRange, TextSelection, ContentLocator or a custom selection object as the first anchor.","commonSituations":"Storing anchors as object and losing track of their concrete type; mixing anchor types produced by different selection processors; deserializing anchors whose type got changed between versions.","solutions":["Pass a TextAnchor instance (or null) as anchor1; cast or convert the selection before calling.","Check anchor1 is TextAnchor before calling; if it is a TextRange, convert it via the appropriate selection processor first.","Catch ArgumentException and treat the merge as failed, returning a fallback anchor."],"exampleFix":"// before\nobject merged;\nTextSelectionHelper.MergeSelections(myTextRange, existingAnchor, out merged);\n// after\nif (myTextRange is TextAnchor first)\n{\n    object merged;\n    TextSelectionHelper.MergeSelections(first, existingAnchor, out merged);\n}\nelse\n{\n    merged = existingAnchor; // skip incompatible anchor\n}","handlingStrategy":"type-guard","validationCode":"if (anchor1 != null && !(anchor1 is TextAnchor)) throw new InvalidOperationException(\"anchor1 must be TextAnchor\");","typeGuard":"static bool IsMergeableAnchor(object a) => a == null || a is TextAnchor;","tryCatchPattern":"try { ok = TextSelectionHelper.MergeSelections(a1, a2, out merged); }\ncatch (ArgumentException ex) when (ex.ParamName == null && ex.Message.StartsWith(\"anchor1\")) { merged = a2; ok = merged != null; }","preventionTips":["Store anchors in strongly typed collections (List<TextAnchor>) instead of object.","Never pass raw TextSelection/TextRange objects where TextAnchor is expected.","Convert selections to TextAnchor via their selection processor at creation time."],"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-21T21:30:21.729Z"}