egametang/ET · error · ArgumentException

Argument_IncompatibleArrayType

Error message

Argument_IncompatibleArrayType

What it means

Standard .NET BCL argument error from SortedSet<T>.CopyTo: the destination array is neither a T[] nor an object[] (after the T[] fast path failed), so there is no compatible element storage to copy into. This branch covers incompatible array ranks/element types caught before assignment.

Source

Thrown at Packages/cn.etetet.core/Scripts/Core/Share/Collection/SortedSet.cs:721

                throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_Index);
            }

            if (array.Length - index < Count)
            {
                throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall);
            }

            T[] tarray = array as T[];
            if (tarray != null)
            {
                CopyTo(tarray, index);
            }
            else
            {
                object[] objects = array as object[];
                if (objects == null)
                {
                    throw new ArgumentException(SR.Argument_IncompatibleArrayType, nameof(array));
                }

                try
                {
                    InOrderTreeWalk(node =>
                    {
                        objects[index++] = node.Item;
                        return true;
                    });
                }
                catch (ArrayTypeMismatchException)
                {
                    throw new ArgumentException(SR.Argument_IncompatibleArrayType, nameof(array));
                }
            }
        }

        #endregion

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Allocate the destination as exactly T[]: `var arr = new T[set.Count];`.
  2. If you must copy to object[], ensure T is a reference type (covariance); value-type sets cannot be copied into object[] via this path.
  3. Convert element types explicitly after copying into a correctly typed array.

Example fix

// before
var dest = new uint[set.Count];
((ICollection<int>)set).CopyTo(dest, 0); // SortedSet<int> -> uint[] rejected
// after
var dest = new int[set.Count];
set.CopyTo(dest, 0);
Defensive patterns

Strategy: validation

Validate before calling

static void CopyToTyped<T>(SortedSet<T> set, Array array, int index)
{
    if (!(array is T[]) && !(array is object[]))
        throw new ArgumentException("array must be T[] or object[]", nameof(array));
    set.CopyTo((T[])array, index);
}

Type guard

static bool IsCompatibleArray<T>(Array a) => a is T[] || a is object[];

Prevention

When it happens

Trigger: Passing an array whose runtime element type is neither T nor object (e.g. copying SortedSet<int> into a uint[], or a base-type array the runtime rejects), or a multidimensional array that already failed the rank check upstream and reaches here.

Common situations: Covariant/contravariant array mistakes; copying a value-type set into an array declared as a different numeric type; passing Array.CreateInstance with a non-matching element type.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/763676e776cd88b3. Report an issue: GitHub.