{"record":{"id":"ed42833067b12504","repo":"stride3d/stride","slug":"the-given-instance-is-a-value-type-and-cannot-have-a-item","errorCode":null,"errorMessage":"The given instance is a value type and cannot have a item ids attached to it.","messagePattern":"The given instance is a value type and cannot have a item ids attached to it\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/Reflection/CollectionItemIdHelper.cs","lineNumber":35,"sourceCode":"        return ShadowObject.Get(instance)?.ContainsKey(CollectionItemIdKey) ?? false;\n    }\n\n    public static bool TryGetCollectionItemIds(object? instance, [MaybeNullWhen(false)] out CollectionItemIdentifiers itemIds)\n    {\n        var shadow = ShadowObject.Get(instance);\n        if (shadow == null)\n        {\n            itemIds = null;\n            return false;\n        }\n\n        itemIds = shadow.TryGetValue(CollectionItemIdKey, out var result) ? (CollectionItemIdentifiers)result : null;\n        return result != null;\n    }\n\n    public static CollectionItemIdentifiers GetCollectionItemIds(object instance)\n    {\n        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));\n\n        var shadow = ShadowObject.GetOrCreate(instance);\n        if (shadow.TryGetValue(CollectionItemIdKey, out var result))\n        {\n            return (CollectionItemIdentifiers)result;\n        }\n\n        var itemIds = new CollectionItemIdentifiers();\n        shadow.Add(CollectionItemIdKey, itemIds);\n        return itemIds;\n    }\n}\n","sourceCodeStart":17,"sourceCodeEnd":48,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/Reflection/CollectionItemIdHelper.cs#L17-L48","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only call GetCollectionItemIds on reference types; check instance.GetType().IsValueType first.","Use the TryGetCollectionItemIds overload which returns false instead of throwing for value types.","If the data is a struct, wrap it in a class or track ids in your own dictionary keyed by the struct value."],"exampleFix":"// before\nvar ids = CollectionItemIdHelper.GetCollectionItemIds(myStructProperty);\n// after\nif (!myStructProperty.GetType().IsValueType)\n{\n    var ids = CollectionItemIdHelper.GetCollectionItemIds(myStructProperty);\n}\nelse\n{\n    CollectionItemIdHelper.TryGetCollectionItemIds(myStructProperty, out _); // returns false safely\n}","handlingStrategy":"type-guard","validationCode":"if (instance is null || instance.GetType().IsValueType)\n    throw new ArgumentException(\"Instance must be a reference type\", nameof(instance));\nvar ids = CollectionItemIdHelper.GetCollectionItemIds(instance);","typeGuard":"static bool CanHaveItemIds(object instance) => !instance.GetType().IsValueType;","tryCatchPattern":"try { ids = CollectionItemIdHelper.GetCollectionItemIds(instance); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"value type\"))\n{\n    ids = null; // value types cannot carry ids\n}","preventionTips":["Check GetType().IsValueType before calling id helpers.","Prefer the TryGetCollectionItemIds overload for uncertain inputs.","Only feed reference-type collection instances (List<T>, class objects) into id tracking.","Unwrap Nullable/boxed structs before calling."],"tags":["reflection","argument-validation","value-types"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}