dotnet/wpf · error · ArgumentException

SR.Format(SR.InvalidLocatorPart, SegmentAttribute +…

Error message

SR.Format(SR.InvalidLocatorPart, SegmentAttribute + segmentNumber.ToString(NumberFormatInfo.InvariantInfo))

What it means

GetLocatorPartSegmentValues throws ArgumentException(SR.InvalidLocatorPart, SegmentAttribute+n) when the segment attribute 'Segment<n>' is missing or its value does not split into exactly two parts (start,end) around the separator. The segment string format is invalid or the attribute is absent.

Solutions

  1. Ensure every segment index 0..Count-1 has a 'Segment<n>' attribute formatted 'start,end' (two parts, invariant numbers)
  2. Create locator parts via TextSelectionProcessor.CreateLocatorPart rather than manual construction
  3. Validate/repair persisted annotation payloads before re-resolving

Example fix

// before
part.NameValuePairs["Segment0"] = "10;25"; // wrong separator
// after
part.NameValuePairs["Segment0"] = "10,25";
Defensive patterns

Strategy: validation

Validate before calling

string seg = part.NameValuePairs["Segment" + n];
bool valid = seg != null && seg.Split(',').Length == 2;
if (!valid) throw new InvalidOperationException($"Segment{n} must be 'start,end'.");

Try / catch

try { GetLocatorPartSegmentValues(part, n, out s, out e); }
catch (ArgumentException) { /* regenerate the locator part from the current selection */ }

Prevention

When it happens

Trigger: Locator part lacks the 'Segment0' (etc.) attribute, or its value contains the wrong number of separators (not exactly one), so segmentString.Split(...) != 2.

Common situations: Hand-edited persisted annotation XML; attributes keyed with wrong culture/invariant formatting; locator parts constructed manually without the 'start,end' format; version drift in serialized locator format.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionProcessor.cs:441

        /// <summary>
        ///     Extracts the values of attributes from a locator part.
        /// </summary>
        /// <param name="locatorPart">the locator part to extract values from</param>
        /// <param name="segmentNumber">the number of the segment to extract values for</param>
        /// <param name="startOffset">value of offset attribute</param>
        /// <param name="endOffset">value of length attribute</param>
        private static void GetLocatorPartSegmentValues(ContentLocatorPart locatorPart, int segmentNumber, out int startOffset, out int endOffset)
        {
            if (segmentNumber < 0)
                throw new ArgumentException(nameof(segmentNumber));

            ReadOnlySpan<char> segmentString = locatorPart.NameValuePairs[SegmentAttribute + segmentNumber.ToString(NumberFormatInfo.InvariantInfo)].AsSpan();

            Span<Range> splitRegions = stackalloc Range[3];
            if (segmentString.Split(splitRegions, Separator) != 2)
            {
                throw new ArgumentException(SR.Format(SR.InvalidLocatorPart, SegmentAttribute + segmentNumber.ToString(NumberFormatInfo.InvariantInfo)));
            }

            startOffset = int.Parse(segmentString[splitRegions[0]], NumberFormatInfo.InvariantInfo);
            endOffset = int.Parse(segmentString[splitRegions[1]], NumberFormatInfo.InvariantInfo);
        }

        /// <summary>
        ///     Returns the TextContainer for a node that contains text.  If the node
        ///     doesn't have a TextContainer this method returns null.
        /// </summary>
        private ITextContainer GetTextContainer(DependencyObject startNode)
        {
            Debug.Assert(startNode != null);

            ITextContainer textContainer = null;
            IServiceProvider serviceProvider = startNode as IServiceProvider;
            if (serviceProvider != null)
            {

View on GitHub (pinned to 81131a70a4)