RSCG – AutoConstructor
| name | AutoConstructor |
| nuget | https://www.nuget.org/packages/AutoConstructor/ |
| link | https://github.com/k94ll13nn3/AutoConstructor |
| author | Kévin Gallienne |
Generating constructor for class with many properties
This is how you can use AutoConstructor .
The code that you start with is
<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="AutoConstructor" Version="4.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
The code that you will use is
using QuickConstructorDemo; var p = new Person("Andrei","Ignat"); Console.WriteLine(p.FullName());
namespace QuickConstructorDemo;
[AutoConstructor]
internal partial class Person
{
private readonly string FirstName;
private readonly string? LastName;
public string FullName() => $"{FirstName} {LastName}";
}
The code that is generated is
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the AutoConstructor source generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
[System.AttributeUsage(System.AttributeTargets.Class,Inherited = false,AllowMultiple = false)]
internal sealed class AutoConstructorAttribute : System.Attribute
{
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the AutoConstructor source generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
[System.AttributeUsage(System.AttributeTargets.Field,Inherited = false,AllowMultiple = false)]
internal sealed class AutoConstructorIgnoreAttribute : System.Attribute
{
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the AutoConstructor source generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
[System.AttributeUsage(System.AttributeTargets.Field,Inherited = false,AllowMultiple = false)]
internal sealed class AutoConstructorInjectAttribute : System.Attribute
{
public AutoConstructorInjectAttribute(string initializer = null,string parameterName = null,System.Type injectedType = null)
{
Initializer = initializer;
ParameterName = parameterName;
InjectedType = injectedType;
}
public string Initializer { get; }
public string ParameterName { get; }
public System.Type InjectedType { get; }
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the AutoConstructor source generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
namespace QuickConstructorDemo
{
partial class Person
{
public Person(string FirstName,string? LastName)
{
this.FirstName = FirstName;
this.LastName = LastName;
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/AutoConstructor
Leave a Reply