AutoConstructor 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package AutoConstructor --version 1.0.1
NuGet\Install-Package AutoConstructor -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AutoConstructor" Version="1.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutoConstructor --version 1.0.1
#r "nuget: AutoConstructor, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AutoConstructor as a Cake Addin
#addin nuget:?package=AutoConstructor&version=1.0.1

// Install AutoConstructor as a Cake Tool
#tool nuget:?package=AutoConstructor&version=1.0.1

AutoConstructor

NuGet GitHub release GitHub license ci.yml

C# source generator that generates a constructor from readonly fields in a class.

Installation

  • Grab the latest package on NuGet.

Requirements

  • Visual Studio 16.9+ is needed
  • .NET SDK 5.0.300+ is needed

How to use

For any class where the generator will be used:

  • Mark the class as partial
  • Use AutoConstructorAttribute on the class

By 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.

Sample

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));
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on AutoConstructor:

Repository Stars
Sergio0694/ComputeSharp
A .NET library to run C# code in parallel on the GPU through DX12, D2D1, and dynamically generated HLSL compute and pixel shaders, with the goal of making GPU computing easy to use for all .NET developers! 🚀
Version Downloads Last updated
5.3.0 902 3/16/2024
5.3.0-beta.2 46 3/15/2024
5.3.0-beta.1 52 3/12/2024
5.2.1 806 3/4/2024
5.2.0 4,297 12/19/2023
5.1.0 5,601 11/23/2023
5.0.1 192 11/22/2023
5.0.0 3,272 11/7/2023
5.0.0-beta.2 59 11/6/2023
5.0.0-beta.1 77 11/2/2023
4.1.1 7,234 7/18/2023
4.1.0 1,219 7/8/2023
4.0.3 6,923 4/24/2023
4.0.2 2,506 4/19/2023
4.0.1 1,554 3/29/2023
4.0.0 320 3/27/2023
3.2.5 5,372 10/15/2022
3.2.4 348 10/7/2022
3.2.3 654 6/28/2022
3.2.2 408 6/27/2022
3.2.1 434 6/11/2022
3.2.0 423 5/20/2022
3.1.1 419 5/14/2022
3.0.0 419 4/1/2022
3.0.0-beta.2 131 3/9/2022
3.0.0-beta.1 978 2/9/2022
2.3.0 490 1/27/2022
2.2.0 416 1/22/2022
2.1.0 408 1/21/2022
2.0.2 312 11/11/2021
1.3.0 416 9/21/2021
1.2.1 320 8/28/2021
1.2.0 322 8/28/2021
1.1.0 313 8/26/2021
1.0.2 394 8/2/2021
1.0.1 443 7/24/2021
1.0.0 368 7/23/2021