Source generator for automatic constructor generation.
$ dotnet add package AutoConstructorC# source generator that generates a constructor from readonly fields in a class.
For any class where the generator will be used:
partialAutoConstructorAttribute on the classBy default, all private readonly without initialization will be used. The will be injected with the same name without any leading _.
Fields marked with AutoConstructorIgnoreAttribute will be ignored.
Use AutoConstructorInjectAttribute to customize the behavior, usualy when the injected parameter and the fields
do not have the same type. It takes three parameters:
Initializer: a string that will be used to initialize the field (by example myService.GetData())ParameterName: the name of the parameter to used in the constructor (by example myService)InjectedType: the type of the parameter to used in the constructor (by example IMyService)When using AutoConstructorInjectAttribute, the parameter name can be shared across multiple fields,
and even use a parameter from another field not annotated with AutoConstructorInjectAttribute, but type must match.
The following code
[AutoConstructor]
partial class Test
{
private readonly string _name;
// Won't be injected
private readonly Uri _uri = new Uri("/non-modified", UriKind.Relative);
// Won't be injected
[AutoConstructorIgnore]
private readonly DateTime _dateNotTaken;
// Won't be injected
private int? _toto;
// Support for nullables
private readonly DateTime? _date;
// Support for generics
private readonly List<DateTime> _items;
// Inject with custom initializer
[AutoConstructorInject("guid.ToString()", "guid", typeof(Guid))]
private readonly string _guidString;
// Use existing parameter defined with AutoConstructorInject
[AutoConstructorInject("guid.ToString().Length", "guid", typeof(Guid))]
private readonly int _guidLength;
// Use existing parameter from a basic injection
[AutoConstructorInject("name.ToUpper()", "name", typeof(string))]
private readonly string _nameShared;
}will generate
public Test(string name, System.DateTime? date, System.Collections.Generic.List<System.DateTime> items, System.Guid guid)
{
this._name = name ?? throw new System.ArgumentNullException(nameof(name));
this._date = date ?? throw new System.ArgumentNullException(nameof(date));
this._items = items ?? throw new System.ArgumentNullException(nameof(items));
this._guidString = guid.ToString() ?? throw new System.ArgumentNullException(nameof(guid));
this._guidLength = guid.ToString().Length;
this._nameShared = name.ToUpper() ?? throw new System.ArgumentNullException(nameof(name));
}| Rule ID | Category | Severity | Notes |
|---|---|---|---|
| ACONS01 | Usage | Warning | ClassWithoutPartialAnalyzer, Documentation |
| ACONS02 | Usage | Warning | ClassWithoutFieldsToInjectAnalyzer, Documentation |
| ACONS03 | Usage | Info | IgnoreAttributeOnNonProcessedFieldAnalyzer, Documentation |
| ACONS04 | Usage | Warning | InjectAttributeOnIgnoredFieldAnalyzer, Documentation |
| ACONS05 | Usage | Warning | IgnoreOrInjectAttributeOnClassWithoutAttributeAnalyzer, Documentation |