{"record":{"id":"7e5dccf1f8a8715b","repo":"stride3d/stride","slug":"the-given-type-does-have-a-public-parameterless-constructor","errorCode":null,"errorMessage":"The given type does have a public parameterless constructor.","messagePattern":"The given type does have a public parameterless constructor\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Annotations/ObjectFactoryAttribute.cs","lineNumber":35,"sourceCode":"    /// <summary>\n    /// The type of the factory to use to create instance of the related type.\n    /// </summary>\n    [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]\n    public Type FactoryType { get; }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"ObjectFactoryAttribute\"/> class.\n    /// </summary>\n    /// <param name=\"factoryType\">The factory type that implements <see cref=\"IObjectFactory\"/>.</param>\n    public ObjectFactoryAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type factoryType)\n    {\n#if NET7_0_OR_GREATER\n        ArgumentNullException.ThrowIfNull(factoryType);\n#else\n        if (factoryType is null) throw new ArgumentNullException(nameof(factoryType));\n#endif // NET7_0_OR_GREATER\n        if (!typeof(IObjectFactory).GetTypeInfo().IsAssignableFrom(factoryType.GetTypeInfo())) throw new ArgumentException($\"The given type does not implement {nameof(IObjectFactory)}/\", nameof(factoryType));\n        if (factoryType.GetTypeInfo().GetConstructor(Type.EmptyTypes) == null) throw new ArgumentException(\"The given type does have a public parameterless constructor.\", nameof(factoryType));\n        FactoryType = factoryType;\n    }\n}\n","sourceCodeStart":17,"sourceCodeEnd":39,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Annotations/ObjectFactoryAttribute.cs#L17-L39","documentation":"After checking that factoryType implements IObjectFactory, ObjectFactoryAttribute requires a public parameterless constructor so the framework can instantiate the factory at runtime. When factoryType.GetConstructor(Type.EmptyTypes) returns null, the constructor throws ArgumentException. Note the message reads 'does have' but the intent is 'does NOT have' — an upstream wording quirk.","triggerScenarios":"Passing a factory class whose only constructors take parameters (e.g. DI-injected dependencies), an abstract factory base class, or a class with a private/internal default constructor.","commonSituations":"DI-style factories with constructor-injected dependencies; abstract base classes passed by mistake; a required constructor parameter added during a refactor.","solutions":["Add a public parameterless constructor to the factory class (it can chain to an overloaded one).","Pass a concrete factory type that constructs its own dependencies.","Ensure the class is not abstract and the default ctor is public, not private/internal."],"exampleFix":"// before\npublic class MeshFactory : IObjectFactory\n{\n    public MeshFactory(ILogger log) { ... } // no parameterless ctor\n}\n\n// after\npublic class MeshFactory : IObjectFactory\n{\n    public MeshFactory() : this(null) { }\n    public MeshFactory(ILogger log) { ... }\n}","handlingStrategy":"validation","validationCode":"if (factoryType.GetConstructor(Type.EmptyTypes) == null)\n    throw new ArgumentException($\"{factoryType} needs a public parameterless constructor\", nameof(factoryType));\nvar attr = new ObjectFactoryAttribute(factoryType);","typeGuard":"static bool HasPublicParameterlessCtor(Type t) => t.GetConstructor(Type.EmptyTypes) != null;\n// compile-time alternative: constrain generics with 'where TFactory : IObjectFactory, new()'","tryCatchPattern":null,"preventionTips":["Always give factory classes a public parameterless constructor, chaining to overloaded ctors if needed.","Avoid passing abstract classes as factory types.","When a factory needs dependencies, resolve them inside the parameterless constructor instead of via ctor injection."],"tags":["csharp","stride-core","serialization","constructor"],"backgroundTag":"missing-parameterless-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"}