dotnet/wpf · error · ArgumentException

SR.WrongSelectionType

Error message

SR.WrongSelectionType (Anchor2: type = {anchor2.GetType()})

What it means

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.

Solutions

  1. Ensure both arguments are TextAnchor (or null) before calling MergeSelections.
  2. Convert incompatible selections through their selection processor to obtain a TextAnchor first.
  3. Catch ArgumentException and handle the merge failure explicitly.

Example fix

// before
TextSelectionHelper.MergeSelections(firstAnchor, someSelection, out merged);
// after
if (someSelection is TextAnchor second)
{
    TextSelectionHelper.MergeSelections(firstAnchor, second, out merged);
}
else
{
    merged = firstAnchor;
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (anchor2 != null && !(anchor2 is TextAnchor)) throw new InvalidOperationException("anchor2 must be TextAnchor");

Type guard

static bool IsMergeableAnchor(object a) => a == null || a is TextAnchor;

Try / catch

try { ok = TextSelectionHelper.MergeSelections(a1, a2, out merged); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Anchor2")) { merged = a1; ok = merged != null; }

Prevention

When it happens

Trigger: Calling MergeSelections with a non-null anchor2 that is not a TextAnchor (e.g. a TextSelection, DataObject, or custom anchor object).

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/acfa0e69f65f1251. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionHelper.cs:75

        ///     Merges the two anchors into one, if possible.
        /// </summary>
        /// <param name="anchor1">anchor to merge </param>
        /// <param name="anchor2">other anchor to merge </param>
        /// <param name="newAnchor">new anchor that contains the data from both 
        /// anchor1 and anchor2</param>
        /// <returns>true if the anchors were merged, false otherwise 
        /// </returns>
        /// <exception cref="ArgumentNullException">anchor1 or anchor2 are null</exception>
        public static bool MergeSelections(Object anchor1, Object anchor2, out Object newAnchor)
        {
            TextAnchor firstAnchor = anchor1 as TextAnchor;
            TextAnchor secondAnchor = anchor2 as TextAnchor;

            if ((anchor1 != null) && (firstAnchor == null))
                throw new ArgumentException(SR.WrongSelectionType, $"anchor1: type = {anchor1.GetType()}");

            if ((anchor2 != null) && (secondAnchor == null))
                throw new ArgumentException(SR.WrongSelectionType, $"Anchor2: type = {anchor2.GetType()}");

            if (firstAnchor == null)
            {
                newAnchor = secondAnchor;
                return newAnchor != null;
            }

            if (secondAnchor == null)
            {
                newAnchor = firstAnchor;
                return newAnchor != null;
            }

            newAnchor = TextAnchor.ExclusiveUnion(firstAnchor, secondAnchor);

            return true;
        }

View on GitHub (pinned to 81131a70a4)