stride3d/stride · error · ArgumentException

The given instance is a value type and cannot have a item…

Error message

The given instance is a value type and cannot have a item ids attached to it.

What it means

Thrown by CollectionItemIdHelper.GetCollectionItemIds when the instance passed is a value type. Item ids are attached via ShadowObject (a reference-keyed side table), so value-type instances cannot carry them.

Solutions

  1. Only call GetCollectionItemIds on reference types; check instance.GetType().IsValueType first.
  2. Use the TryGetCollectionItemIds overload which returns false instead of throwing for value types.
  3. If the data is a struct, wrap it in a class or track ids in your own dictionary keyed by the struct value.

Example fix

// before
var ids = CollectionItemIdHelper.GetCollectionItemIds(myStructProperty);
// after
if (!myStructProperty.GetType().IsValueType)
{
    var ids = CollectionItemIdHelper.GetCollectionItemIds(myStructProperty);
}
else
{
    CollectionItemIdHelper.TryGetCollectionItemIds(myStructProperty, out _); // returns false safely
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (instance is null || instance.GetType().IsValueType)
    throw new ArgumentException("Instance must be a reference type", nameof(instance));
var ids = CollectionItemIdHelper.GetCollectionItemIds(instance);

Type guard

static bool CanHaveItemIds(object instance) => !instance.GetType().IsValueType;

Try / catch

try { ids = CollectionItemIdHelper.GetCollectionItemIds(instance); }
catch (ArgumentException ex) when (ex.Message.Contains("value type"))
{
    ids = null; // value types cannot carry ids
}

Prevention

When it happens

Trigger: Calling GetCollectionItemIds(valueTypeInstance) — e.g. passing a struct, int, or enum instead of a reference-type collection/object such as a List<T> or class instance.

Common situations: Reflecting over property values generically without checking the property type; retrieving ids from a struct-typed collection property; passing a boxed copy rather than the original reference object.

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

Appendix: source

Thrown at sources/assets/Stride.Core.Assets/Reflection/CollectionItemIdHelper.cs:35

        return ShadowObject.Get(instance)?.ContainsKey(CollectionItemIdKey) ?? false;
    }

    public static bool TryGetCollectionItemIds(object? instance, [MaybeNullWhen(false)] out CollectionItemIdentifiers itemIds)
    {
        var shadow = ShadowObject.Get(instance);
        if (shadow == null)
        {
            itemIds = null;
            return false;
        }

        itemIds = shadow.TryGetValue(CollectionItemIdKey, out var result) ? (CollectionItemIdentifiers)result : null;
        return result != null;
    }

    public static CollectionItemIdentifiers GetCollectionItemIds(object instance)
    {
        if (instance.GetType().IsValueType) throw new ArgumentException(@"The given instance is a value type and cannot have a item ids attached to it.", nameof(instance));

        var shadow = ShadowObject.GetOrCreate(instance);
        if (shadow.TryGetValue(CollectionItemIdKey, out var result))
        {
            return (CollectionItemIdentifiers)result;
        }

        var itemIds = new CollectionItemIdentifiers();
        shadow.Add(CollectionItemIdKey, itemIds);
        return itemIds;
    }
}

View on GitHub (pinned to 96fad776d2)