AutoCtor by Cameron MacFarland
Nuget / site data
Details
Info
Name: AutoCtor
Source Generator to automatically create a constructor with all the readonly fields set.
Author: Cameron MacFarland
NuGet: https://www.nuget.org/packages/AutoCtor/
You can find more details at
Original Readme
AutoCtor
AutoCtor is a Roslyn Source Generator that will automatically create a constructor for your class for use with constructor Dependency Injection.
NuGet packages
https://nuget.org/packages/AutoCtor/
Usage
Your code
[AutoConstruct]
public partial class ExampleClass
{
private readonly ICustomService _customService;
}
What gets generated
partial class ExampleClass
{
public ExampleClass(ICustomService customService)
{
_customService = customService;
}
}
More examples
You can also initialize readonly fields, and AutoCtor will not include them in the constructor.
[AutoConstruct]
public partial class ClassWithInitializer
{
private readonly ICustomService _customService;
private readonly IList<string> _list = new List<string>();
}
partial class ClassWithInitializer
{
public ClassWithInitializer(ICustomService customService)
{
_customService = customService;
// no code to set _list
}
}
If there is a single base constructor with parameters, AutoCtor will include that base constructor in the constructor it creates.
public abstract class BaseClass
{
protected IAnotherService _anotherService;
public BaseClass(IAnotherService anotherService)
{
_anotherService = anotherService;
}
}
[AutoConstruct]
public partial class ClassWithBase : BaseClass
{
private readonly ICustomService _customService;
}
partial class ClassWithBase
{
public ClassWithBase(IAnotherService anotherService, ICustomService customService) : base(anotherService)
{
_customService = customService;
}
}
Embedding the attributes in your project
By default, the [AutoConstruct]
attributes referenced in your project are contained in an external dll. It is also possible to embed the attributes directly in your project. To do this, you must do two things:
- Define the MSBuild constant
AUTOCTOR_EMBED_ATTRIBUTES
. This ensures the attributes are embedded in your project. - Add
compile
to the list of excluded assets in your<PackageReference>
element. This ensures the attributes in your project are referenced, insted of the AutoCtor.Attributes.dll library.
Your project file should look like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Define the MSBuild constant -->
<DefineConstants>AUTOCTOR_EMBED_ATTRIBUTES</DefineConstants>
</PropertyGroup>
<!-- Add the package -->
<PackageReference Include="AutoCtor"
PrivateAssets="all"
ExcludeAssets="compile;runtime" />
<!-- ☝ Add compile to the list of excluded assets. -->
</Project>
Preserving usage of the [AutoConstruct]
attribute
The [AutoConstruct]
attributes are decorated with the [Conditional]
attribute, so their usage will not appear in the build output of your project. If you use reflection at runtime you will not find the [AutoConstruct]
attributes.
If you wish to preserve these attributes in the build output, you can define the AUTOCTOR_USAGES
MSBuild variable.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Define the MSBuild constant -->
<DefineConstants>AUTOCTOR_USAGES</DefineConstants>
</PropertyGroup>
<!-- Add the package -->
<PackageReference Include="AutoCtor" PrivateAssets="all" />
</Project>
About
Generate constructor from non-initialized fields
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references AutoCtor
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoCtor" Version="1.0.0" />
</ItemGroup>
</Project>
This is the use of AutoCtor in Program.cs
// See https://aka.ms/new-console-template for more information
using AutoCtorDemo;
Console.WriteLine("Hello, World!");
var p = new Person("Andrei", "Ignat");
This is the use of AutoCtor in Person.cs
using AutoCtor;
namespace AutoCtorDemo;
[AutoConstruct]
internal partial class Person
{
private readonly string FirstName;
private readonly string? LastName;
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- AutoConstructAttribute.g.cs
- AutoCtorDemo.Person.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/distantcam/AutoCtor
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#if AUTOCTOR_EMBED_ATTRIBUTES
namespace AutoCtor
{
[System.Runtime.CompilerServices.CompilerGenerated]
[System.AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional("AUTOCTOR_USAGES")]
internal sealed class AutoConstructAttribute : System.Attribute
{
[System.Runtime.CompilerServices.CompilerGenerated]
public AutoConstructAttribute()
{
}
}
}
#endif
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/distantcam/AutoCtor
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace AutoCtorDemo
{
partial class Person
{
public Person(string FirstName, string LastName)
{
this.FirstName = FirstName;
this.LastName = LastName;
}
}
}
Usefull
Download Example (.NET C# )
Share AutoCtor
https://ignatandrei.github.io/RSCG_Examples/v2/docs/AutoCtor