Skip to main content

RSCG_IFormattable by Andrei Ignat

Nuget / site data

NugetNuget GitHub last commit GitHub Repo stars

Details

Info

Original Readme

note

RSCG_IFormattable

Roslyn Code Generator for IFormattable

Usage

Add to your csproj file:

<ItemGroup>
<PackageReference Include="RSCG_IFormattable" Version="2024.711.2013" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="RSCG_IFormattableCommon" Version="2024.711.2013" />
</ItemGroup>

Add to your source file:

[RSCG_IFormattableCommon.AddIFormattable]
internal partial class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
}

And then the result is

using RSCG_IFormattableConsole;

Person person = new ();
person.FirstName = "Andrei";
person.LastName = "Ignat";

Console.WriteLine(person.ToString("The person name is {FirstName} {LastName}",null));

About

note

Generating .ToString for IFormattable

How to use

Example ( source csproj, source files )

This is the CSharp Project that references RSCG_IFormattable

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

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

<ItemGroup>
<PackageReference Include="RSCG_IFormattable" Version="2024.711.2013" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="RSCG_IFormattableCommon" Version="2024.711.2013" />
</ItemGroup>

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


</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

namespace RSCG_IFormattableDemo {

partial class Person {
public string ToString(string format){
return ToString(format, System.Globalization.CultureInfo.CurrentCulture);
}
public string ToString(string format, IFormatProvider provider) {
if (String.IsNullOrEmpty(format)) format = "G";
if (provider == null) provider = System.Globalization.CultureInfo.CurrentCulture;
string retValue=format;
if(format=="G") {
retValue ="";

retValue += FirstName + " = " + FirstName + ";";

retValue += LastName + " = " + LastName + ";";
return retValue;
}

if(format.Contains("{FirstName}")){
var val = FirstName;
retValue=retValue.Replace("{FirstName}", val.ToString());
}

if(format.Contains("{LastName}")){
var val = LastName;
retValue=retValue.Replace("{LastName}", val.ToString());
}
return retValue;
}
}


}

Usefull

Download Example (.NET C# )

Share RSCG_IFormattable

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

In the same category (Templating) - 9 other generators

Gobie

InterceptorTemplate

JKToolKit.TemplatePropertyGenerator

Microsoft.NET.Sdk.Razor.SourceGenerators

Minerals.AutoMixins

MorrisMoxy

RazorBlade

RSCG_Templating

spreadcheetah