RSCG – OptionToStringGenerator
| name | OptionToStringGenerator |
| nuget | https://www.nuget.org/packages/Seekatar.OptionToStringGenerator/ |
| link | https://github.com/Seekatar/OptionToStringGenerator |
| author | Jim W |
Generating similar ToString method for classes with many properties. It can also generate for external classes.
This is how you can use OptionToStringGenerator .
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>
<ItemGroup>
<PackageReference Include="Seekatar.OptionToStringGenerator" Version="0.3.1" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
The code that you will use is
using Class2String; using Seekatar.OptionToStringGenerator; var p = new Person(); p.FirstName = "Andrei"; p.LastName = "Ignat"; p.Age = 50; Console.WriteLine(p.OptionsToString());
using Seekatar.OptionToStringGenerator;
namespace Class2String;
[OptionsToString]
internal class Person
{
[OutputMask(PrefixLen = 3)]
public string? FirstName { get; set; }
[OutputMask(SuffixLen = 3)]
public string? LastName { get; set; }
public string FUllName => $"{FirstName} {LastName}";
[OutputIgnore]
public int Age { get; set; }
}
The code that is generated is
#nullable enable
using static Seekatar.Mask;
namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Class2String.Person o)
{
return $@"Class2String.Person:
FirstName : {Format(o?.FirstName,prefixLen:3,suffixLen:0)}
LastName : {Format(o?.LastName,prefixLen:0,suffixLen:3)}
FUllName : {Format(o?.FUllName)}
";
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/OptionToStringGenerator
Leave a Reply