RSCG – ConstructorGenerator

RSCG – ConstructorGenerator
 
 

name ConstructorGenerator
nuget https://www.nuget.org/packages/ConstructorGenerator/
link https://github.com/Swarley97/ConstructorGenerator
author Swarley97

Generate constructor for classes

 

This is how you can use ConstructorGenerator .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>

	<ItemGroup>
	  <PackageReference Include="ConstructorGenerator" Version="1.0.2" />
	</ItemGroup>
</Project>


The code that you will use is


using QuickConstructorDemo;

var p = new Person("Andrei", "Ignat");

Console.WriteLine(p.FullName());


using ConstructorGenerator.Attributes;

namespace QuickConstructorDemo;

[GenerateFullConstructor]
internal partial class Person
{
    [ConstructorDependency]
    private readonly string FirstName="";

    private readonly string? LastName;
    
    public string FullName() => $"{FirstName} {LastName}";
    
}


 

The code that is generated is

namespace QuickConstructorDemo
{
	internal partial class Person 
	{
		public Person(string firstName, string lastName) 
		{ 
			FirstName = firstName;
			LastName = lastName; 
			OnConstructing();
		}
		partial void OnConstructing();
	}
}

Code and pdf at

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