{"record":{"id":"acfa0e69f65f1251","repo":"dotnet/wpf","slug":"sr-wrongselectiontype-anchor2-type-anchor2-gettype","errorCode":null,"errorMessage":"SR.WrongSelectionType (Anchor2: type = {anchor2.GetType()})","messagePattern":"SR\\.WrongSelectionType \\(Anchor2: type = (.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionHelper.cs","lineNumber":75,"sourceCode":"        ///     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\n            return true;\n        }\n","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionHelper.cs#L57-L93","documentation":"ArgumentException sentinel from TextSelectionHelper.MergeSelections: the second anchor (anchor2) is not a selection type the helper can merge (the message embeds anchor2's actual type), e.g. it is neither an ITextRange nor a TextAnchor, so the two anchors cannot be combined.","triggerScenarios":"Calling MergeSelections with a non-null anchor2 that is not a TextAnchor (e.g. a TextSelection, DataObject, or custom anchor object).","commonSituations":"Merging annotations from different sources where one anchor was created by a non-text selection processor; caching anchors as object and passing the wrong instance.","solutions":["Ensure both arguments are TextAnchor (or null) before calling MergeSelections.","Convert incompatible selections through their selection processor to obtain a TextAnchor first.","Catch ArgumentException and handle the merge failure explicitly."],"exampleFix":"// before\nTextSelectionHelper.MergeSelections(firstAnchor, someSelection, out merged);\n// after\nif (someSelection is TextAnchor second)\n{\n    TextSelectionHelper.MergeSelections(firstAnchor, second, out merged);\n}\nelse\n{\n    merged = firstAnchor;\n}","handlingStrategy":"type-guard","validationCode":"if (anchor2 != null && !(anchor2 is TextAnchor)) throw new InvalidOperationException(\"anchor2 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.Message.StartsWith(\"Anchor2\")) { merged = a1; ok = merged != null; }","preventionTips":["Keep both anchors from the same selection processor so types match.","Check both arguments with `is TextAnchor` before merging.","Avoid boxing/unboxing anchors across serialization boundaries that lose type fidelity."],"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"}