dotnet/wpf · error · ArgumentException

array

Error message

array

What it means

CopyTo(array, arrayIndex) throws ArgumentException("array") when the array's element type cannot hold TextElementType instances — either the array is a non-array object (GetElementType returns null) or its element type is not assignable from TextElementType. Copying text elements into, say, a string[] or int[] is rejected up front.

Solutions

  1. Allocate the target array with the collection's element type: new TextElementType[list.Count].
  2. Use OfType<T>().ToArray() or CopyTo into a typed array instead of manually constructing one.
  3. Verify array.GetType().GetElementType().IsAssignableFrom(typeof(TextElementType)) before calling CopyTo.
  4. Catch ArgumentException and retry with a correctly typed array.

Example fix

// before
var arr = new string[list.Count];
((ICollection)list).CopyTo(arr, 0);
// after
var arr = new Paragraph[list.Count];
list.CopyTo(arr, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array.GetType().GetElementType()?.IsAssignableFrom(typeof(TextElement)) != true) throw new ArgumentException("Incompatible array element type", nameof(array));

Type guard

static bool IsCompatibleArray(Array a) => a.GetType().GetElementType()?.IsInstanceOfType(Activator.CreateInstance(a.GetType().GetElementType()!)) ?? false;

Try / catch

try { list.CopyTo(arr, 0); } catch (ArgumentException) { arr = new TextElementType[list.Count]; list.CopyTo(arr, 0); }

Prevention

When it happens

Trigger: Calling CopyTo with an array whose element type is narrower or unrelated to the collection's element type (e.g. string[], object[] is fine only if assignable — unrelated types throw).

Common situations: Copying into a generic object[] where the element type check fails on strict array covariance rules; passing a 2D array or jagged array whose GetElementType is an array type; copy/paste utility code that assumed a compatible buffer.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

                    this.TextContainer.EndChange();
                }
            }
        }

        #endregion IList Members

        #region ICollection Members

        void ICollection.CopyTo(Array array, int arrayIndex)
        {
            int count = this.Count;

            ArgumentNullException.ThrowIfNull(array);

            Type elementType = array.GetType().GetElementType();
            if (elementType == null || !elementType.IsAssignableFrom(typeof(TextElementType)))
            {
                throw new ArgumentException("array");
            }

            ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);

            if (arrayIndex > array.Length)
            {
                throw new ArgumentException("arrayIndex");
            }

            if (array.Length < arrayIndex + count)
            {
                throw new ArgumentException(SR.Format(SR.TextElementCollection_CannotCopyToArrayNotSufficientMemory, count, arrayIndex, array.Length));
            }

            for (TextElementType element = (TextElementType)this.FirstChild; element != null; element = (TextElementType)element.NextElement)
            {
                array.SetValue(element, arrayIndex++);
            }

View on GitHub (pinned to 81131a70a4)