ConstructorGenerator is a C# source generator which generates constructors for you.
Install the ConstructorGenerator NuGet package.
Install-Package ConstructorGenerator
dotnet add package ConstructorGenerator
ConstructorGenerator requires C# 9.0 or later.
Add the [GenerateFullConstructor]
attribute to your class or struct and a constructor will be generated.
[GenerateFullConstructor]
public partial class MyClass
{
private readonly ILogger _logger;
private readonly IDependencyA _dependency;
private readonly IDependencyB _dependencyB;
}
The generated constructor will look like this:
public MyClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB)
{
_logger = logger;
_dependency = dependency;
_dependencyB = dependencyB;
}
Only for the following members a constructor parameter will be generated
- Read only fields with no initializer
- Get only Properties or init only properties with no initializer
- Properties or fields with the
[ConstructorDependency]
attribute (this override the rules above)
Examples
// ignored
private readonly IDependency _dependency = new Dependency();
// not-ignored
private readonly IDependency _dependency;
// not-ignored
[ConstructorDependency]
private readonly IDependency _dependency = new Dependency();
// ignored
private readonly IDependency _dependency { get; } = new Dependency();
// ignored
private readonly IDependency _dependency { get; set; }
// not-ignored
private readonly IDependency _dependency { get; init; }
// not-ignored
[ConstructorDependency]
private readonly IDependency _dependency { get; set; }
You can ignore properties and fields by adding the [ExcludeConstructorDependency]
attribute to them.
[GenerateFullConstructor]
public partial class MyClass
{
private readonly ILogger _logger;
private readonly IDependencyA _dependency;
[ExcludeConstructorDependency]
private readonly IDependencyB _dependencyB;
}
The generated constructor will look like this:
public MyClass(ILogger logger, IDependencyA dependency)
{
_logger = logger;
_dependency = dependency;
}
You can explicitly specify the properties and fields which should be included in the generated constructor by adding the [ConstructorDependency]
attribute to them.
This can be useful for the following cases:
- If you do not want to use the
[GenerateFullConstructor]
attribute on the class or struct - If you want to add additional constructor dependencies which are not included by default (see Rules)
- If you want to specify it as optional parameter (see Optional parameters)
public partial class MyClass
{
[ConstructorDependency]
private readonly ILogger _logger = new NullLogger();
[ConstructorDependency]
private readonly IDependencyA _dependency;
private readonly IDependencyB _dependencyB;
}
The generated constructor will look like this:
public MyClass(ILogger logger, IDependencyA dependency)
{
_logger = logger;
_dependency = dependency;
}
ConstructorGenerator supports inheritance. You can put the [GenerateFullConstructor]
attribute on a derived class.
[GenerateFullConstructor]
public MyClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB)
{
_logger = logger;
_dependency = dependency;
_dependencyB = dependencyB;
}
[GenerateFullConstructor]
public partial class MyDerivedClass : MyClass
{
public IDependencyC DependencyC { get; }
}
The generated constructor will look like this:
public MyDerivedClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB, IDependencyC dependencyC)
: base(logger, dependency, dependencyB)
{
DependencyC = dependencyC;
}
Or, if you just want to generate the base constructor call (for example because your derived class have no constructor dependencies),
you can put the [GenerateBaseConstructorCall]
attribute on the derived class.
// constructor of MyClass
public MyClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB)
{
_logger = logger;
_dependency = dependency;
_dependencyB = dependencyB;
}
[GenerateBaseConstructorCall]
public partial class MyDerivedClass : MyClass
{
}
The generated constructor will look like this:
public MyDerivedClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB)
: base(logger, dependency, dependencyB)
{
}
Note: It doesn't matter whether the base class constructor is also generated by the ConstructorGenerator or manually defined in the code. In either case, the constructor call will be generated.
[GenerateFullConstructor]
public partial class MyClass
{
private readonly ILogger _logger;
private readonly IDependencyA _dependency;
[ConstructorDependency(IsOptional = true)]
private readonly IDependencyB _dependencyB;
}
The generated constructor will look like this:
public MyClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB = null)
{
_logger = logger;
_dependency = dependency;
_dependencyB = dependencyB;
}
To execute code in the constructor (like attaching events or something like this) you can implement the partial method OnConstructing
which will be called after all fields and properties has been set.
[GenerateFullConstructor]
public partial class MyClass
{
private readonly ILogger _logger;
private readonly IDependencyA _dependency;
private readonly IDependencyB _dependencyB;
partial void OnConstructing()
{
// do constructor stuff
_logger.Log("Constructor called");
}
}
ConstructorGenerator supports specifying the accessibility of the generated constructor. You can put the [ConstructorAccessibility]
attribute on a class or struct and the constructor will be generated with the specified accessibility.
[GenerateFullConstructor]
[ConstructorAccessibility(Accessibility.Internal)]
public partial class MyClass
{
private readonly ILogger _logger;
private readonly IDependencyA _dependency;
private readonly IDependencyB _dependencyB;
}
The generated constructor will look like this:
internal MyClass(ILogger logger, IDependencyA dependency, IDependencyB dependencyB)
{
_logger = logger;
_dependency = dependency;
_dependencyB = dependencyB;
}
ConstructorGenerator is licensed under the MIT license.