RSCG_IFormattable by Andrei Ignat
Nuget / site data
Details
Info
info
Name: RSCG_IFormattable
Implement IFormattable for a class
Author: Andrei Ignat
NuGet: https://www.nuget.org/packages/RSCG_IFormattable/
https://www.nuget.org/packages/RSCG_IFormattableCommon/
You can find more details at https://github.com/ignatandrei/rscg_iformattable/
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 )
- CSharp Project
- Program.cs
- Person.cs
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>
This is the use of RSCG_IFormattable in Program.cs
using RSCG_IFormattableDemo;
Person person = new();
person.FirstName = "Andrei";
person.LastName = "Ignat";
Console.WriteLine(person.ToString("The person name is {FirstName} {LastName}"));
This is the use of RSCG_IFormattable in Person.cs
namespace RSCG_IFormattableDemo;
[RSCG_IFormattableCommon.AddIFormattable]
internal partial class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Person.gen.cs
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