{"record":{"id":"2f0d2ccc71211ae0","repo":"stride3d/stride","slug":"failed-to-create-instance-of-type-type-type-does-not-have-a","errorCode":null,"errorMessage":"Failed to create instance of type '{type}', type does not have a parameterless constructor.","messagePattern":"Failed to create instance of type '(.+?)', type does not have a parameterless constructor\\.","errorType":"exception","errorClass":"InstanceCreationException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs","lineNumber":123,"sourceCode":"            type = GetDefaultImplementation(type);\n\n            // We can't instantiate primitives or arrays\n            if (PrimitiveDescriptor.IsPrimitive(type) || type.IsArray)\n                throw new InstanceCreationException($\"Failed to create instance of type '{type}', wrong factory.\");\n\n            if (type.GetConstructor(EmptyTypes) != null || type.IsValueType)\n            {\n                try\n                {\n                    return Activator.CreateInstance(type);\n                }\n                catch (Exception e)\n                {\n                    throw new InstanceCreationException($\"'{typeof(Activator)}' failed to create instance of type '{type}', see inner exception.\", e);\n                }\n            }\n\n            throw new InstanceCreationException($\"Failed to create instance of type '{type}', type does not have a parameterless constructor.\");\n        }\n\n        public class InstanceCreationException : Exception\n        {\n            public InstanceCreationException(string message) : base(message) { }\n            public InstanceCreationException(string message, Exception innerException) : base(message, innerException) { }\n        }\n    }\n}\n","sourceCodeStart":105,"sourceCodeEnd":133,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs#L105-L133","documentation":"DefaultObjectFactory.Create falls through to this error when the type has no accessible parameterless constructor, so Activator.CreateInstance cannot be used. The YAML/JSON deserializer needs to allocate instances of nodes' target types, and with no default constructor there is no way to do so without extra construction information.","triggerScenarios":"Deserializing YAML/JSON into a class that only has parameterized constructors; a type whose parameterless constructor is private or internal; struct-less factory registration for a type with custom constructors.","commonSituations":"Immutable/config classes written with constructor injection only after a refactor; third-party POCO types lacking default ctors; types created via factory patterns where the deserializer was never told about the factory.","solutions":["Add a public parameterless constructor to the target type","Register a custom IObjectFactory (or LambdaObjectFactory) that constructs the type with its required arguments","Use the overload/flow that deserializes into an existing instance (e.g. DeserializeInto) instead of creating one","Make the parameterless constructor public if it exists but is non-public"],"exampleFix":"// before\nclass Config { public Config(string name) { Name = name; } }\n// after\nclass Config { public Config() { } public Config(string name) { Name = name; } }","handlingStrategy":"type-guard","validationCode":"if (type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null) == null)\n    throw new InvalidOperationException($\"{type.Name} needs a public parameterless constructor for deserialization\");","typeGuard":"static bool HasDefaultCtor(Type t) => t?.GetConstructor(Type.EmptyTypes) != null;","tryCatchPattern":"try { return serializer.Deserialize(stream, expectedType); }\ncatch (DefaultObjectFactory.InstanceCreationException ex) { throw new InvalidOperationException($\"{expectedType} lacks a parameterless constructor\", ex); }","preventionTips":["Add public parameterless constructors to all serialized types","Register IObjectFactory implementations for immutable types","Use DeserializeInto for types that only have parameterized constructors","Add a unit test that instantiates every serializable type via Activator"],"tags":["yaml","serialization","instantiation","constructor"],"backgroundTag":"missing-default-constructor","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"}