dotnet/wpf · error · ArgumentException

SR.Format(SR.TextElementCollection_ItemHasUnexpectedType…

Error message

SR.Format(SR.TextElementCollection_ItemHasUnexpectedType, "range", typeof(TextElementType).Name, typeof(TextElementType).Name)

What it means

Thrown by TextElementCollection<T>.AddRange when an item yielded by the range cannot be cast to the collection's element type TextElementType. Each enumerated element must be assignable to the collection's item type.

Solutions

  1. Filter the source to only compatible items: range.OfType<TextElementType>() before AddRange.
  2. Convert incompatible items into appropriate TextElement instances.
  3. Use a strongly typed IEnumerable<TextElementType> source so mismatches surface at compile time.

Example fix

// before
collection.AddRange(heterogeneousList);
// after
collection.AddRange(heterogeneousList.OfType<TextElementType>());
Defensive patterns

Strategy: type-guard

Validate before calling

var items = range as IEnumerable<TextElementType>
    ?? range?.Cast<object>().Where(TextElementType_instance => TextElementType_instance is TextElementType).Cast<TextElementType>().ToList();
if (items != null) collection.AddRange(items);

Type guard

static bool IsCompatible(object o) => o is TextElementType;
// usage: collection.AddRange(heterogeneous.Where(IsCompatible).Cast<TextElementType>());

Try / catch

try { collection.AddRange(range); }
catch (ArgumentException ex) when (ex.Message.Contains("unexpected type")) { /* fall back to filtered OfType<TextElementType>() insert */ }

Prevention

When it happens

Trigger: Calling AddRange on a heterogeneous enumerable (e.g. ArrayList or non-generic IEnumerable) containing items that are not TextElementType instances; the cast 'as TextElementType' yields null.

Common situations: Passing mixed-type legacy collections; iterating a shared list that holds non-text objects; type changes after refactoring the element type parameter.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs:335

            IEnumerator enumerator = range.GetEnumerator();
            if (enumerator == null)
            {
                throw new ArgumentException(SR.TextElementCollection_NoEnumerator, nameof(range));
            }

            this.TextContainer.BeginChange();
            try
            {
                while (enumerator.MoveNext())
                {
                    TextElementType element = enumerator.Current as TextElementType;

                    if (element == null)
                    {
                        // REVIEW: It would be better design if we reviewed all elements in the range before starting an insert.
                        // Otherwise, we might insert half the elements and then throw.
                        throw new ArgumentException(SR.Format(SR.TextElementCollection_ItemHasUnexpectedType, "range", typeof(TextElementType).Name, typeof(TextElementType).Name), "value");
                    }

                    Add(element);
                }
            }
            finally
            {
                this.TextContainer.EndChange();
            }
        }

        //...................................................................
        //
        //  IEnumerable<TextElementType> Members
        //
        //...................................................................

        #region IEnumerable<TextElementType> Members

View on GitHub (pinned to 81131a70a4)