stride3d/stride · error · ArgumentException

The index must be an int.

Error message

The index must be an int.

What it means

OldCollectionDescriptor.GetValue(object list, object index) mirrors ListDescriptor's boxed-index overload: it requires an int index and throws ArgumentException 'The index must be an int.' for any other boxed numeric or non-numeric type before delegating to the int overload.

Solutions

  1. Cast/convert the index to int before the call: GetValue(list, (int)longIndex).
  2. Parse string indices with int.TryParse beforehand.
  3. Prefer the int-indexed GetValue(list, int) overload directly when the index type is known.

Example fix

// before
var v = descriptor.GetValue(collection, i as object); // i is long
// after
var v = descriptor.GetValue(collection, (int)i);
Defensive patterns

Strategy: type-guard

Validate before calling

if (index is int i)
    descriptor.GetValue(collection, i);
else if (index is long l && l <= int.MaxValue)
    descriptor.GetValue(collection, (int)l);

Type guard

static bool IsValidCollectionIndex(object? index) => index is int;

Try / catch

try { var v = descriptor.GetValue(collection, index); }
catch (ArgumentException ex) when (ex.Message == "The index must be an int.") { var v = descriptor.GetValue(collection, Convert.ToInt32(index)); }

Prevention

When it happens

Trigger: Calling OldCollectionDescriptor.GetValue(collection, index) with a boxed long/short/string index; generic enumeration code that widened indices to long.

Common situations: Legacy serialization/editor code paths using OldCollectionDescriptor with object-typed indices coming from dynamic APIs or 64-bit counters.

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 stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/6e8a612a57176df0. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Reflection/TypeDescriptors/OldCollectionDescriptor.cs:146

        IsPureCollection = Count == 0;
    }

    public override DescriptorCategory Category => DescriptorCategory.Collection;

    /// <summary>
    /// Gets a value indicating whether this collection implements <see cref="IList"/> or <see cref="IList{T}"/>.
    /// </summary>
    public bool IsList { get; }

    /// <summary>
    /// Returns the value matching the given index in the collection.
    /// </summary>
    /// <param name="list">The list.</param>
    /// <param name="index">The index.</param>
    public override object? GetValue(object list, object index)
    {
        ArgumentNullException.ThrowIfNull(list);
        if (index is not int) throw new ArgumentException("The index must be an int.");
        return GetValue(list, (int)index);
    }

    /// <summary>
    /// Returns the value matching the given index in the collection.
    /// </summary>
    /// <param name="list">The list.</param>
    /// <param name="index">The index.</param>
    public override object? GetValue(object list, int index)
    {
        ArgumentNullException.ThrowIfNull(list);
        return getIndexedItemMethod(list, index);
    }

    public override void SetValue(object list, object index, object? value)
    {
        ArgumentNullException.ThrowIfNull(list);
        if (index is not int) throw new ArgumentException("The index must be an int.");

View on GitHub (pinned to 96fad776d2)