JKToolKit.TemplatePropertyGenerator by Jonas Kamsker
NuGet / site data
Details
Info
Name: JKToolKit.TemplatePropertyGenerator
Generates strongly typed stringformatters based on stringformats.
Author: Jonas Kamsker
NuGet: https://www.nuget.org/packages/JKToolKit.TemplatePropertyGenerator/
You can find more details at https://github.com/JKamsker/JKToolKit.TemplatePropertyGenerator
Source: https://github.com/JKamsker/JKToolKit.TemplatePropertyGenerator
Original Readme
Overview
TemplatePropertyGenerator is a C# source generator that creates strongly-typed template properties from annotated classes. This allows for easy and safe string formatting by generating classes with methods to format predefined templates.
Features
- Define template properties with format strings using attributes.
 - Auto-generate classes with methods to format the strings.
 - Supports FormattableString for more complex formatting needs.
 
How to Use
- 
Install the NuGet package:
Ensure you have the NuGet package installed in your project. If not, you can install it via the NuGet Package Manager or the .NET CLI.
dotnet add package JKToolKit.TemplatePropertyGenerator - 
Define your templates:
Use the
TemplatePropertyattribute to define your template properties in a partial class. The attribute takes the name of the property and the format string as parameters.[TemplateProperty("Hello", "Hello {value}, {value}!")]
[TemplateProperty("FooBar", "Foo {value}, Bar {value}")]
public static partial class Consts
{
} - 
Use the generated code:
Once the generator runs, it will produce a class with properties and methods to format your strings. You can use these in your code as shown below:
private static void Main(string[] args)
{
Console.WriteLine(Consts.Hello.Template); // Output: Hello {value}, {value}!
Console.WriteLine(Consts.Hello.Format("World")); // Output: Hello World, World!
Console.WriteLine(Consts.FooBar.Format("Foo1", "Bar1")); // Output: Foo Foo1, Bar Bar1
} 
Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue to discuss any changes or improvements.
License
This project is licensed under the MIT License .
Acknowledgements
About
String templating for a class
How to use
Example (source csproj, source files)
- CSharp Project
 - Program.cs
 - Person.cs
 
This is the CSharp Project that references JKToolKit.TemplatePropertyGenerator
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="JKToolKit.TemplatePropertyGenerator" Version="0.0.4" />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>
This is the use of JKToolKit.TemplatePropertyGenerator in Program.cs
using SimpleTemplate;
Person person = new();
person.FirstName = "Andrei";
person.LastName = "Ignat";
Console.WriteLine(new Person.HelloClass().Format(person.FirstName,person.LastName));
This is the use of JKToolKit.TemplatePropertyGenerator in Person.cs
namespace SimpleTemplate;
[TemplateProperty("Hello", "Hello {name1}, {name2}!")]
internal partial class Person
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- TemplatePropertyAttribute.g.cs
 - SimpleTemplate.Person_Generated.cs
 
global using JKToolKit.TemplatePropertyGenerator.Attributes;
using System;
namespace JKToolKit.TemplatePropertyGenerator.Attributes;
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
internal class TemplatePropertyAttribute : Attribute
{
    public string Name { get; }
    public string Format { get; }
    public TemplatePropertyAttribute(string name, string format)
    {
        Name = name;
        Format = format;
    }
}
// <auto-generated />
using System;
namespace SimpleTemplate
{
    internal partial class Person
    {
        public static readonly HelloClass Hello = new();
        public class HelloClass
        {
            public string Template => "Hello {name1}, {name2}!";
            internal HelloClass()
            {
            }
            public string Format(string name1, string name2)
            {
                return $"Hello {name1}, {name2}!";
            }
            public FormattableString AsFormattable(string name1, string name2)
            {
                return $"Hello {name1}, {name2}!";
            }
        }
    }
}
Useful
Download Example (.NET C#)
Share JKToolKit.TemplatePropertyGenerator
https://ignatandrei.github.io/RSCG_Examples/v2/docs/JKToolKit.TemplatePropertyGenerator
aaa
Category "Templating" has the following generators:
1 Gobie
4 JKToolKit.TemplatePropertyGenerator
5 Microsoft.NET.Sdk.Razor.SourceGenerators