Skip to content

GenerateConstructorAttribute

NexusLabs.Needlr.Generators

GenerateConstructorAttribute Class

Generates a public constructor for a partial class from its eligible private instance readonly fields, eliminating hand-written constructor, field-assignment, and guard-clause boilerplate.

public sealed class GenerateConstructorAttribute : System.Attribute

Inheritance System.Object 🡒 System.Attribute 🡒 GenerateConstructorAttribute

Example

[GenerateConstructor]
public partial class UserService
{
    private readonly IRepository _repository;
}

// Generated:
// public UserService(IRepository repository)
// {
//     _repository = repository;
// }
[GenerateConstructor]
public partial class MyWorker : BackgroundService
{
    private readonly IRepository _repository;

    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        return Task.CompletedTask;
    }
}

// BackgroundService has an accessible parameterless constructor, so the generated
// constructor relies on the implicit base() call:
// public MyWorker(IRepository repository)
// {
//     _repository = repository;
// }

Remarks

The generator produces exactly one public constructor whose parameters are derived, in declaration order, from every eligible field: a private, instance, readonly field without an initializer, unless the field carries ConstructorIgnoreAttribute. Field names using the _camelCase convention are mapped to camelCase parameter names.

This attribute never introduces optional parameters or overloads on the generated constructor itself. Use an explicit hand-written constructor overload, or a get-only property with its own initialization logic, when optional construction paths are required.

The containing class must be declared partial and must not declare its own instance constructor, because Roslyn source generators can only add new members to a partial type -- they cannot rewrite or inject statements into a user-authored constructor. The containing type must also not be nested. It may derive from System.Object directly, or from any base type that itself has an accessible (public or protected) parameterless constructor -- including the common case of a base type with no explicit constructors at all -- since the generated constructor relies on the implicit : base() call. This supports common framework base types such as BackgroundService. A base type that requires constructor arguments is unsupported: no source is emitted, and an analyzer reports the requirement at compile time instead.

A field-level positive guard attribute -- a built-in or custom ConstructorGuardAttribute, or a custom alias attribute defined with ConstructorGuardDefinitionAttribute -- also enables constructor generation with None, even when this attribute is not applied to the class.

Constructors

GenerateConstructorAttribute() Constructor

Initializes a new instance of the GenerateConstructorAttribute class with None. No automatic null guards are emitted.

public GenerateConstructorAttribute();

GenerateConstructorAttribute(ConstructorNullGuardMode) Constructor

Initializes a new instance of the GenerateConstructorAttribute class with an explicit null-guard mode.

public GenerateConstructorAttribute(NexusLabs.Needlr.Generators.ConstructorNullGuardMode mode);

Parameters

mode ConstructorNullGuardMode

Controls whether the generator automatically emits null guards for eligible non-nullable reference-type fields.

Properties

GenerateConstructorAttribute.Mode Property

Gets the configured null-guard mode for the generated constructor.

public NexusLabs.Needlr.Generators.ConstructorNullGuardMode Mode { get; }

Property Value

ConstructorNullGuardMode