RSCG – PrimaryParameter
RSCG – PrimaryParameter
name | PrimaryParameter |
nuget | https://www.nuget.org/packages/FaustVX.PrimaryParameter.SG |
link | https://github.com/FaustVX/PrimaryParameter |
author | FaustVX |
Generating properties from .NET 8 constructor parameters
This is how you can use PrimaryParameter .
The code that you start with is
<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>
The code that you will use is
using QuickConstructorDemo; var p = new Person("Andrei", "Ignat"); Console.WriteLine(p.FullName());
using PrimaryParameter.SG; namespace QuickConstructorDemo; internal partial class Person([Property]string FirstName,[Field(Name ="_LastName",Scope ="public")]string? LastName=null) { //private readonly string FirstName; //private readonly string? LastName; public string FullName() => $"{FirstName} {_LastName}"; }
The code that is generated is
namespace QuickConstructorDemo { partial class Person { public string FirstName { get; init; } = FirstName; } } namespace QuickConstructorDemo { partial class Person { public readonly string _LastName = LastName; } }
using global::System; namespace PrimaryParameter.SG { [AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)] sealed class FieldAttribute : Attribute { public string Name { get; init; } public string AssignFormat { get; init; } public Type Type { get; init; } public bool IsReadonly { get; init; } public string Scope { get; init; } } }
using global::System; namespace PrimaryParameter.SG { [AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)] sealed class PropertyAttribute : Attribute { public string Name { get; init; } public string AssignFormat { get; init; } public Type Type { get; init; } public bool WithInit { get; init; } public string Scope { get; init; } } }
using global::System; namespace PrimaryParameter.SG { [AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = true)] sealed class RefFieldAttribute : Attribute { public string Name { get; init; } public string Scope { get; init; } public bool IsReadonlyRef { get; init; } public bool IsRefReadonly { get; init; } } }
Code and pdf at https://ignatandrei.github.io/RSCG_Examples/v2/docs/PrimaryParameter
Leave a Reply