dotnet/wpf · error · ArgumentException

SR.Format(SR.UnexpectedParameterType, o.GetType()…

Error message

SR.Format(SR.UnexpectedParameterType, o.GetType(), typeof(FlowPosition))

What it means

FlowPosition.CompareTo throws ArgumentException when the argument is not a FlowPosition. This method compares two flow positions to order them within the flow content; it is called during fixed-document node generation (GetFixedNodesForFlowRange).

Solutions

  1. Guarantee all objects passed to CompareTo are FlowPosition instances
  2. Separate heterogeneous position types into distinct typed collections
  3. Use pattern matching to narrow before comparing

Example fix

// before
list.Sort(); // mixed element types
// after
list.Sort((a, b) => ((FlowPosition)a).CompareTo((FlowPosition)b)); // after ensuring all elements are FlowPosition
Defensive patterns

Strategy: type-guard

Validate before calling

if (position is FlowPosition fp) { result = thisPosition.CompareTo(fp); }

Type guard

static bool IsFlowPosition(object o) => o is FlowPosition;

Try / catch

try { a.CompareTo(b); } catch (ArgumentException) { /* b is not a FlowPosition */ }

Prevention

When it happens

Trigger: Comparing a FlowPosition to an object of another type, e.g. through Array.Sort, List<T>.BinarySearch, or direct calls passing mismatched objects.

Common situations: Custom pagination or fixed-flow-generation code that stores positions alongside other location objects (like TextPointer or ContentPosition) in one collection and sorts it.

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/c0d8b9b191da6377. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FlowPosition.cs:69

        /// <summary>
        /// Create a shallow copy of this objet
        /// </summary>
        /// <returns>A clone of this FlowPosition</returns>
        public object Clone()
        {
            return new FlowPosition(_container, _flowNode, _offset);
        }


        // Compare two FixedTextPointer based on their flow order and offset
        public int CompareTo(object o)
        {
            ArgumentNullException.ThrowIfNull(o);

            FlowPosition flow = o as FlowPosition;
            if (flow == null)
            {
                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, o.GetType(), typeof(FlowPosition)), nameof(o));
            }

            return _OverlapAwareCompare(flow);
        }


        /// <summary>
        /// Compute hash code. A flow position is predominantly identified
        /// by its flow node and the offset.
        /// </summary>
        /// <returns>int - hash code</returns>
        public override int GetHashCode()
        {
            return _flowNode.GetHashCode()^_offset.GetHashCode();
        }


#if DEBUG

View on GitHub (pinned to 81131a70a4)