Skip to content

Repository files navigation

Primary Parameter

NuGet version (FaustVX.PrimaryParameter.SG)Update NuGet

Description

Using a Field, RefField, Property or DoNotUse 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

partialclassC([Field(Name="_a",AssignFormat="{0}.ToString()",Type=typeof(string)),Field(Name=nameof(C._b)),Field,Property(Setter="init",Summary="Documentation")]inti)// 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/// <summary>/// Documentation/// </summary>// private int I { get; init; } = i; // generated Property
# endregion
publicvoidM0(){i++;// error on usage of iConsole.WriteLine(i);// error on usage of i}publicvoidM1(){vari=0;i++;// don't error on usage of localsConsole.WriteLine(_i);// automaticaly created readonly fieldConsole.WriteLine(_a);// automaticaly created readonly field based on Name propertyConsole.WriteLine(I);// automaticaly created readonly property}}refpartialstruct Ref([RefField(IsReadonlyRef=false,IsRefReadonly=false),RefField(Name=nameof(Ref.I),Scope="public")]inti){
# region Generated members
privaterefint_i=refi;publicreadonlyref readonly intI=refi;
# endregion
}

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

You can type as many attributes as you want on a single parameter (Except for DoNotUse).

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
SummaryThe <summary> documentation tagnull
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
SummaryThe <summary> documentation tagnull
PropertyNameProperty to modify the generated field nameI (for a parameter named i)
SetterTo generate the set, init or neither accessor along the getinit
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
SummaryThe <summary> documentation tagnull
WithoutBackingStorageGenerate the body of the accessors, without a backing storagefalse
DoNotUseAllowInMemberInitChange to allow the use of parameter in member initializertrue

Reported Diagnostics

CodeTitleMessageSeverity
PC01Accessing a Primary ParameterCan't access a primary parameter ('{0}') with a [Field], [RefField], [Property] or [DoNotUse] attribute, use {1}Error
PC02Attribute generate nothingUse this attribute only on primary parameterWarning
PC03Attribute generate nothingThis member's name ('{0}') is already usedWarning
Error when a member's name is already used in the type
PC04RefField in non ref structCan't apply [RefField] in non ref struct '{0}'Error
PC05RefField on non ref parameterCan't apply [RefField] on non ref parameter '{0}'Error

.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_DefaultSetterShould properties generates with set, init or neither accessorinit

Versions

VersionDateComments
v1.9.123/11/2024Fixed bug (emitting the non fully-qualified type name)
v1.9.023/11/2024Fixed bug when the Type property was not specified with Arrays
v1.8.117/11/2024Automatically detect partial properties
v1.8.015/11/2024Added IsPartial for Property
v1.7.023/06/2024Added WithoutBackingStorage for Property
v1.6.022/06/2024Fix a bug: accept arrays
v1.5.119/05/2024Modified some errors message
v1.5.018/05/2024Don't generate PC01 on a primary contructor base call
v1.4.018/05/2024Added Conditional on generated attributes
v1.3.418/05/2024Added <summary> generation
v1.3.301/12/2023Renamed DontUse to DoNotUse
v1.3.219/11/2023Don't generate the partial generated type if not needed
v1.3.119/11/2023Fix a bug with member initialization
v1.3.019/11/2023Added DontUseAttribute
Add a code-fix for CS0282
Changed PropertyAttribute.WithInit to PropertyAttribute.Setter
v1.2.025/08/2023Support for default values customization
v1.1.015/08/2023dotnet/roslyn#67371 fixed
(related to v0.4.6)
v1.0.001/08/2023Added code-fixes
v0.4.716/07/2023Don't error on nameof access or inside the same argument list usage
v0.4.6.116/07/2023Fix typos in Readme.md
v0.4.616/07/2023Added RefField attribute
Currently uses Unsafe.AsRef() due to a compiler bug dotnet/roslyn#67371
v0.4.518/05/2023Added Scope and IsReadonly properties on Field
Scope property on Property defaulted to public
v0.4.428/04/2023Added AssignFormat and Type properties
v0.4.327/04/2023Field and Property now have using global::System
v0.4.226/04/2023Bug-fix with previous update
v0.4.125/04/2023More precise warnings/errors location
v0.4.022/04/2023Added PC02 and PC03 warnings/errors
v0.3.121/04/2023Added Scope property on Property attribute
Attributes are internal
v0.3.020/04/2023Added Property attribute
v0.2.020/04/2023Support for Name fields and multiple Field
v0.1.019/04/2023Initial release

About

Using a Field or Property attribute on parameters. Automaticaly generate private readonly fields or private properties. Forbid the use of primary constructor's parameters.

Topics

Resources

Stars

11 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages