Skip to main content

AutoCtor by Cameron MacFarland

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

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

Source : https://github.com/distantcam/AutoCtor

Original Readme

note

AutoCtor

Build Status NuGet Status

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:

  1. Define the MSBuild constant AUTOCTOR_EMBED_ATTRIBUTES. This ensures the attributes are embedded in your project.
  2. 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

note

Generate constructor from non-initialized fields

How to use

Example ( source csproj, source files )

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>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

//------------------------------------------------------------------------------
// <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

Usefull

Download Example (.NET C# )

Share AutoCtor

https://ignatandrei.github.io/RSCG_Examples/v2/docs/AutoCtor

In the same category (Constructor) - 5 other generators

AutoConstructor

AutoDeconstruct

PrimaryParameter

QuickConstructor

sourcedepend