Skip to main content

RSCG_Templating by Andrei Ignat

Nuget / site data

NugetNuget GitHub last commit GitHub Repo stars

Details

Info

Original Readme

note

RSCG_Templating

Templating for generating everything from classes, methods from a Roslyn Code Generator

Templating is in SCRIBAN form

How to use

Add reference to

  <ItemGroup>
<PackageReference Include="RSCG_Templating" Version="2023.1007.724" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="RSCG_TemplatingCommon" Version="2023.1007.724" />
</ItemGroup>
<!-- this is just for debug purposes -->
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<!-- those are the templates files, see IGenerateDataFromClass -->
<ItemGroup>
<AdditionalFiles Include="ClassTypeName.txt" />
<AdditionalFiles Include="ClassPropByName.txt" />
</ItemGroup>

Then add additional files , for example

//autogenerated by RSCG_Templating version {{data.Version}} from file {{fileName}}
namespace {{data.nameSpace}} {

partial class {{data.className}} {
public string MyTypeName = "{{data.nameSpace}}.{{data.className}}";
}//end class

}//end namespace

Now add

//can have multiple attributes on partial classes
[IGenerateDataFromClass("ClassTypeName")]
public partial class Person

Advanced uses

For the moment , RSCG_Templating generates definition for a class with properties + methods . See example for generating enum from properties and setting properties by name

var x = new Person();
Console.WriteLine("The generated string type is "+x.MyTypeName);
x.FirstName = "Andrei";
//set last name via prop
x.SetPropValue(ePerson_Properties.LastName, "Ignat");
Console.WriteLine("called directly first name : " + x.FirstName);
Console.WriteLine("called via enum of prop first name : " + x.GetPropValue(ePerson_Properties.FirstName));
Console.WriteLine("called get property :" + x.GetPropValue(ePerson_Properties.Name));

See example at https://github.com/ignatandrei/RSCG_Templating/tree/main/src/RSCG_Templating

More templates

  1. Template for having the class type name: ClassTypeName
  2. Template for having the class properties as enum : ClassPropByName
  3. Template for setting properties after name : ClassPropByName

About

note

Templating every your data ( starting with class)

How to use

Example ( source csproj, source files )

This is the CSharp Project that references RSCG_Templating

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

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="ClassTypeName.txt" />
<AdditionalFiles Include="ClassPropByName.txt" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="RSCG_Templating" Version="2023.1022.1748" OutputItemType="Analyzer" />
<PackageReference Include="RSCG_TemplatingCommon" Version="2023.1022.1748" />

</ItemGroup>

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

</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

//autogenerated by RSCG_Templating version 2023.1022.1748.0 from file Microsoft.CodeAnalysis.AdditionalTextFile
namespace RSCG_TemplatingDemo {
public enum ePerson_Properties {
None = 0,

Name,

FirstName,

LastName,

}
partial class Person {

public object GetPropValue(ePerson_Properties prop){
switch(prop){

case ePerson_Properties.Name:

return this.Name;


case ePerson_Properties.FirstName:

return this.FirstName;


case ePerson_Properties.LastName:

return this.LastName;


default:
throw new NotImplementedException();
}
}
public void SetPropValue<T>(ePerson_Properties prop , T value){
switch(prop){

case ePerson_Properties.Name:

throw new NotImplementedException();


case ePerson_Properties.FirstName:

this.FirstName = (string?)(dynamic)value;
break;


case ePerson_Properties.LastName:

this.LastName = (string?)(dynamic)value;
break;


default:
throw new NotImplementedException();
}
}
}//end class

}//end namespace

Usefull

Download Example (.NET C# )

Share RSCG_Templating

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

In the same category (Templating) - 10 other generators

Gobie

InterceptorTemplate

JKToolKit.TemplatePropertyGenerator

Microsoft.NET.Sdk.Razor.SourceGenerators

Minerals.AutoMixins

MorrisMoxy

RazorBlade

RazorSlices

RSCG_IFormattable

spreadcheetah