Skip to main content

CommonCodeGenerator by yamaokunousausa

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: CommonCodeGenerator

Common code generator.

Author: yamaokunousausa

NuGet: https://www.nuget.org/packages/CommonCodeGenerator/

You can find more details at https://github.com/usausa/common-code-generator

Source : https://github.com/usausa/common-code-generator

Original Readme

note

CommonCodeGenerator

NuGet Badge

Reference

Add reference to CommonCodeGenerator and CommonCodeGenerator.SourceGenerator to csproj.

  <ItemGroup>
<PackageReference Include="CommonCodeGenerator" Version="0.2.0" />
<PackageReference Include="CommonCodeGenerator.SourceGenerator" Version="0.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

ToString

Source

[GenerateToString]
public partial class Data
{
public int Id { get; set; }

public string Name { get; set; } = default!;

public int[] Values { get; set; } = default!;

[IgnoreToString]
public int Ignore { get; set; }
}

Result


var data = new Data { Id = 123, Name = "xyz", Values = [1, 2] };
var str = data.ToString();
Assert.Equal("{ Id = 123, Name = xyz, Values = [1, 2] }", str);

About

note

Generating ToString from classes

How to use

Example ( source csproj, source files )

This is the CSharp Project that references CommonCodeGenerator

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

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommonCodeGenerator" Version="0.2.0" />
<PackageReference Include="CommonCodeGenerator.SourceGenerator" Version="0.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

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

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

// <auto-generated />
#nullable disable
namespace ToStringData
{
partial class Person
{
public override string ToString()
{
var handler = new global::System.Runtime.CompilerServices.DefaultInterpolatedStringHandler(0, 0, default, stackalloc char[256]);
handler.AppendLiteral("Person ");
handler.AppendLiteral("{ ");
handler.AppendLiteral("FirstName = ");
handler.AppendFormatted(FirstName);
handler.AppendLiteral(", ");
handler.AppendLiteral("LastName = ");
handler.AppendFormatted(LastName);
handler.AppendLiteral(" }");
return handler.ToStringAndClear();
}
}
}

Usefull

Download Example (.NET C# )

Share CommonCodeGenerator

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

In the same category (EnhancementClass) - 25 other generators

ApparatusAOT

AspectGenerator

DudNet

Enhanced.GetTypes

FastGenericNew

HsuSgSync

Immutype

Ling.Audit

Lombok.NET

M31.FluentAPI

MemoryPack

Meziantou.Polyfill

Microsoft.Extensions.Logging

Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator

Microsoft.Interop.JavaScript.JSImportGenerator

OptionToStringGenerator

QueryStringGenerator

RSCG_Decorator

RSCG_UtilityTypes

StaticReflection

SyncMethodGenerator

System.Runtime.InteropServices

System.Text.RegularExpressions

TelemetryLogging

ThisClass