Skip to main content

PrimaryParameter by FaustVX

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: PrimaryParameter

Package Description

Author: FaustVX

NuGet: https://www.nuget.org/packages/FaustVX.PrimaryParameter.SG

You can find more details at https://github.com/FaustVX/PrimaryParameter

Source : https://github.com/FaustVX/PrimaryParameter

Original Readme

note

Primary Parameter

NuGet version (FaustVX.PrimaryParameter.SG) Update NuGet

Description

Using a Field or RefField or Property attribute on parameters.

Automatically generate private readonly fields or private readonly ref readonly fields or public properties.

Forbid the use of primary constructor's parameters.

Usage

partial class C([Field(Name = "_a", AssignFormat = "{0}.ToString()", Type = typeof(string)), Field(Name = nameof(C._b)), Field, Property(WithInit = true)]int i) // type must be partial, but can be class / struct
{
# region Generated members
// private readonly string _a = i.ToString(); // generated field (with type and formated assignment)
// private readonly int _b = i; // generated field (with computed name)
// private readonly int _i = i; // generated field
// private int { get; init; } = i; // generated Property
# endregion

public void M0()
{
i++; // error on usage of i
Console.WriteLine(i); // error on usage of i
}

public void M1()
{
var i = 0;
i++; // don't error on usage of locals
Console.WriteLine(_i); // automaticaly created readonly field
Console.WriteLine(_a); // automaticaly created readonly field based on Name property
Console.WriteLine(I); // automaticaly created readonly property
}
}

ref partial struct Ref([RefField(IsReadonlyRef = false, IsRefReadonly = false), RefField(Name = nameof(Ref.I), Scope = "public")]int i)
{
# region Generated members
private ref int _i = ref i;
public readonly ref readonly int I = ref i;
# endregion
}

To enable the feature, type [Field] or [RefField] or [Property] before the primary parameter you want.

You can type as many attributes as you want on a single parameter.

Attribute Properties

AttributePropertyCommentsDefault value
FieldNameProperty to modify the generated field name_i (for a parameter named i)
IsReadnolyTo generate the readonly modifiertrue
ScopeTo change the scope of the generated propertyprivate
AssignFormatTo change the assignment for that field{0}
TypeTo change the type for that fieldsame type as parameter
RefFieldNameProperty to modify the generated field name_i (for a parameter named i)
IsReadnolyRefTo generate the readonly ref modifiertrue
IsRefReadnolyTo generate the ref readonly modifiertrue
ScopeTo change the scope of the generated propertyprivate
PropertyNameProperty to modify the generated field nameI (for a parameter named i)
WithInitTo generate the init accessor along the getfalse
ScopeTo change the scope of the generated propertypublic
AssignFormatTo change the assignment for that property{0}
TypeTo change the type for that propertysame type as parameter

.csproj properties

PropertyDescriptionDefault value
Fields
PrimaryParameter_Field_DefaultScopeThe default scope for fields generationprivate
PrimaryParameter_Field_DefaultReadonlyShould fields generates with readonly modifiertrue
Ref Fields
PrimaryParameter_RefField_DefaultScopeThe default scope for ref field generationprivate
PrimaryParameter_RefField_DefaultReadonlyRefShould ref fields generates with readonly ref modifiertrue
PrimaryParameter_RefField_DefaultRefReadonlyShould ref fields generates with ref readonly modifiertrue
Properties
PrimaryParameter_Property_DefaultScopeThe default scope for properties generationpublic
PrimaryParameter_Property_DefaultWithInitShould properties generates with init accessortrue

About

note

Generating properties from .NET 8 constructor parameters

How to use

Example ( source csproj, source files )

This is the CSharp Project that references PrimaryParameter

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

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

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

<ItemGroup>
<PackageReference Include="FaustVX.PrimaryParameter.SG" Version="1.2.0" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

namespace QuickConstructorDemo
{
partial class Person
{
public string FirstName { get; init; } = FirstName;
}
}
namespace QuickConstructorDemo
{
partial class Person
{
public readonly string _LastName = LastName;
}
}

Usefull

Download Example (.NET C# )

Share PrimaryParameter

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

In the same category (Constructor) - 5 other generators

AutoConstructor

AutoCtor

AutoDeconstruct

QuickConstructor

sourcedepend