CommonCodeGenerator by yamaokunousausa
NuGet / site data
Details
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
Original Readme
CommonCodeGenerator
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
Generating ToString from classes
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
- Person.cs
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>
This is the use of CommonCodeGenerator in Program.cs
using ToStringData;
Console.WriteLine("Hello, World!");
Person person = new ();
person.FirstName = "Andrei";
person.LastName = "Ignat";
Console.WriteLine(person.ToString());
This is the use of CommonCodeGenerator in Person.cs
using CommonCodeGenerator;
namespace ToStringData;
[GenerateToString]
internal partial class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
[IgnoreToString]
public int Age { get; set; }
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- ToStringData_Person_ToString.g.cs
// <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();
}
}
}
Useful
Download Example (.NET C#)
Share CommonCodeGenerator
https://ignatandrei.github.io/RSCG_Examples/v2/docs/CommonCodeGenerator
aaa
Category "EnhancementClass" has the following generators:
1 ApparatusAOT
2023-04-16
2 AspectGenerator
2024-01-07
3 CommonCodeGenerator
2024-04-03
4 Comparison
2025-05-25
5 DudNet
2023-10-27
6 Enhanced.GetTypes
2024-09-17
7 FastGenericNew
2023-08-10
8 Immutype
2023-08-12
9 Ling.Audit
2023-12-12
10 Lombok.NET
2023-04-16
11 M31.FluentAPI
2023-08-25
12 MemberAccessor
2025-03-24
13 MemoryPack
2023-08-04
14 Meziantou.Polyfill
2023-10-10
15 Microsoft.Extensions.Logging
2023-04-16
16 Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator
2023-11-17
17 Microsoft.Interop.JavaScript.JSImportGenerator 2023-04-16
18 OptionToStringGenerator
2024-02-15
19 Program
2025-11-06
20 QueryStringGenerator
2024-11-07
21 RSCG_Decorator
2023-09-30
22 RSCG_UtilityTypes
2023-12-22
23 StaticReflection
2023-10-13
24 SyncMethodGenerator
2023-08-14
25 System.Runtime.InteropServices
2023-04-16
26 System.Text.RegularExpressions
2023-04-16
27 TelemetryLogging
2023-11-30
28 ThisClass
2024-04-19