{"record":{"id":"0b8a38f5b8b114a5","repo":"stride3d/stride","slug":"the-type-of-collection-does-not-have-a-parameterless","errorCode":null,"errorMessage":"The type of collection does not have a parameterless constructor.","messagePattern":"The type of collection does not have a parameterless constructor\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/Yaml/CollectionWithIdsSerializer.cs","lineNumber":104,"sourceCode":"            if (!identifier.TryGet(i, out var id))\n            {\n                id = ItemId.New();\n                identifier.Add(i, id);\n            }\n            instance.Add(id, item);\n            ++i;\n        }\n\n        return instance;\n    }\n\n    /// <inheritdoc/>\n    protected override IDictionary CreatEmptyContainer(ITypeDescriptor descriptor)\n    {\n        var collectionDescriptor = (CollectionDescriptor)descriptor;\n        var type = typeof(CollectionWithItemIds<>).MakeGenericType(collectionDescriptor.ElementType);\n        if (type.GetConstructor(Type.EmptyTypes) == null)\n            throw new InvalidOperationException(\"The type of collection does not have a parameterless constructor.\");\n        return (IDictionary)Activator.CreateInstance(type)!;\n    }\n\n    /// <inheritdoc/>\n    protected override void TransformAfterDeserialization(IDictionary container, ITypeDescriptor targetDescriptor, object targetCollection, ICollection<ItemId>? deletedItems = null)\n    {\n        var collectionDescriptor = (CollectionDescriptor)targetDescriptor;\n        var type = typeof(CollectionWithItemIds<>).MakeGenericType(collectionDescriptor.ElementType);\n        if (!type.IsInstanceOfType(container))\n            throw new InvalidOperationException(\"The given container does not match the expected type.\");\n        var identifier = CollectionItemIdHelper.GetCollectionItemIds(targetCollection);\n        identifier.Clear();\n\n        // The collection may contain some initial data from its containing instance's ctor,\n        // let's replace the existing data with the data we have serialized\n        collectionDescriptor.Clear(targetCollection);\n\n        var i = 0;","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/Yaml/CollectionWithIdsSerializer.cs#L86-L122","documentation":"CollectionWithIdsSerializer.CreatEmptyContainer builds a closed generic CollectionWithItemIds<T> over the target collection's element type; it demands that constructed type to have a parameterless constructor so it can instantiate the deserialization container. If CollectionWithItemIds<T> (or the reflected construction) lacks an accessible parameterless ctor, it throws InvalidOperationException.","triggerScenarios":"Serializing/deserializing an asset collection whose element type makes CollectionWithItemIds<T> resolve to a type without a public parameterless constructor; fork modifications removing the default ctor; generic type resolution producing an unexpected element type (e.g. abstract/interface element without concrete container).","commonSituations":"Custom asset types with unusual element types in item collections; modified Stride source where CollectionWithItemIds<T> gained ctor parameters; reflection/AOT trimming scenarios stripping the constructor.","solutions":["Keep a public parameterless constructor on CollectionWithItemIds<T> (restore it if removed)","Fix the element type resolution so the generic is built over the correct concrete type","Check whether linking/AOT trimming removed the ctor and preserve it (DynamicallyAccessedMembers/linker XML)","Verify you are not passing a custom collection type into the asset pipeline that bypasses the expected container"],"exampleFix":"// before (CollectionWithItemIds.cs)\npublic CollectionWithItemIds(int capacity) { ... } // no parameterless ctor\n// after\npublic CollectionWithItemIds() { }\npublic CollectionWithItemIds(int capacity) { ... }","handlingStrategy":"validation","validationCode":"var elemType = GetCollectionElementType(myCollectionType);\nvar containerType = typeof(CollectionWithItemIds<>).MakeGenericType(elemType);\nif (containerType.GetConstructor(Type.EmptyTypes) == null)\n    throw new InvalidOperationException($\"{containerType} lost its parameterless ctor; fix before asset serialization\");","typeGuard":null,"tryCatchPattern":"try { SerializeAssets(stream, asset); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"parameterless constructor\"))\n{\n    Log.Error(\"Serializer container type broken: {Message}\", ex.Message);\n    throw;\n}","preventionTips":["Never remove the default constructor from CollectionWithItemIds<T>","Add a reflection test asserting the container types have parameterless ctors","Configure trimmer/linker preserve rules for serializer container types","Keep element types concrete and consistent with the serializer's expectations"],"tags":["csharp","invalidoperationexception","yaml","serialization","reflection"],"backgroundTag":"invalid-constructor-argument","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"}