ConstructorGenerator 1.0.0
See the version list below for details.
dotnet add package ConstructorGenerator --version 1.0.0
NuGet\Install-Package ConstructorGenerator -Version 1.0.0
<PackageReference Include="ConstructorGenerator" Version="1.0.0" />
paket add ConstructorGenerator --version 1.0.0
#r "nuget: ConstructorGenerator, 1.0.0"
// Install ConstructorGenerator as a Cake Addin #addin nuget:?package=ConstructorGenerator&version=1.0.0 // Install ConstructorGenerator as a Cake Tool #tool nuget:?package=ConstructorGenerator&version=1.0.0
ConstructorGenerator
ConstructorGenerator is a C# source generator which generates constructors for you.
Installation
Install the ConstructorGenerator NuGet package.
Package Manager Console
Install-Package ConstructorGenerator
.NET CLI
dotnet add package ConstructorGenerator
Getting started
Prerequisites
ConstructorGenerator requires C# 9.0 or later.
Usage
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;
}
Rules
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; }
Ignoring properties and fields
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;
}
Explicitly specifying properties and fields
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;
}
Inheritance
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.
Optional parameters
[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;
}
Execute code in the generated constructor
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");
}
}
Constructor accessibility
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;
}
License
ConstructorGenerator is licensed under the MIT license.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.