CodeGen.Extensions 1.0.3

dotnet add package CodeGen.Extensions --version 1.0.3
                    
NuGet\Install-Package CodeGen.Extensions -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CodeGen.Extensions" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeGen.Extensions" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="CodeGen.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CodeGen.Extensions --version 1.0.3
                    
#r "nuget: CodeGen.Extensions, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CodeGen.Extensions@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CodeGen.Extensions&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=CodeGen.Extensions&version=1.0.3
                    
Install as a Cake Tool

πŸ“ C# Source Generation Library

A lightweight, performance-oriented library that provides efficient building blocks for C# source generation.

⚠️ Note: This project is still a work in progress. It is designed with future .NET (β‰₯10) source generator scenarios in mind.

✨ Features

  • ⚑ Fast, zero/low-allocation text writer (CodeTextWriter)
  • 🧱 Composable code builders on top of the writer (CodeBuilder)
  • πŸ“ Fluent APIs for common code-generation patterns (indents, bodies, blocks)
  • πŸ”’ ref struct design ensures stack-only safety and avoids heap allocations
  • Useful extensions to be used for rosyln apis

πŸ“¦ Nuget packages

Project/Package name Nuget
CodeGen.Writing Nuget
CodeGen.Extensions Nuget

πŸ“š Contents

βœοΈπŸ“βš‘CodeWriter (least abstract way of generating code)

CodeWriter is essentially a zero/low-allocation text writer implemented as a ref struct. It comes with helper methods like OpenBody(), CloseBody(), UpIndent(), and DownIndent().

You can also take advantage of interpolated string handlers so interpolated values never allocate on the heap before writing.

// example with indent of 3 spaces
var writer = new CodeTextWriter(
   stackalloc char[256], stackalloc char[128],
   3, ' ');
string result;

try
{
   const string str = "Test";

   writer.WriteLineInterpolated($"public class {str}");
   writer.OpenBody();

   writer.WriteLine("public string StrTest { get; set; }");
   writer.WriteLine();

   // does not allocate the interpolated string
   writer.WriteLineInterpolated($"public {str}()");
   writer.OpenBody();
   writer.WriteLine($"StrTest = \"Test\";");
   writer.CloseBody();

   writer.CloseBody();
}
finally
{
   // if you need the result as a real heap string you will have to call ToString
   // if you only need a ReadOnlySpan<char> u can always access writer.WrittenSpan
   result = writer.ToString();
   writer.Dispose();
}

return result;

πŸ§±πŸ”§πŸ—οΈ CodeBuilder (some more useful helpers)

While CodeWriter is minimal and explicit, CodeBuilder introduces convenience helpers for common patterns you often repeat when generating C#.

For example:

  • Automatic handling of class/namespace declarations
  • Simplified method/property scaffolding
  • Higher-level composition without losing performance guarantees
var builder = new CodeBuilder(
   stackalloc char[1024], stackalloc char[128],
   3, ' ');
try 
{
   // ... build
}
finally 
{
   builder.Dispose();
}

The CodeBuilder offers all methods of the CodeTextWriter too, and you can chain them together, since all of them return the CodeBuilder or Module as ref in a fluent fashion.

🌐 File Module of the CodeBuilder

Offers most cases for the top of the file that is being generated. Here is a very common example

  • Writes the auto generated comment
  • Writes #nullable enable
  • Writes usings for this file
builder.File
   .WriteStartAutoGenerated()
      .WriteLine("// Here")
      .WriteLine("// is a message")
      .WriteLineInterpolated($"// Test {x}")
   .WriteEndAutoGenerated()
   .WriteNullableEnable()
      .WriteUsing("NameSpaceA")
      .WriteUsing("NameSpaceB.Test", true);

πŸ›οΈ TypeHeader Module of the CodeBuilder

This can hel you writing the header part of classes/structs/interfaces. It supports:

  • Access Modifier
  • Modifiers
  • Generic Parameters / Constraints
  • Primary Constructor
  • Base List

Here is a comprehensive example:

builder.TypeHeader
   .WriteAccessInternal()
   .WriteClassModifiers(ClassModifier.Sealed | ClassModifier.Partial)
   .WriteClass("ClassName")
   .WriteStartGenericParameters()
      .WriteGenericParameter("T")
      .WriteGenericParameter("T2", true)
   .WriteEndGenericParameters()
   .WriteStartParameterList()
      .WriteParameter("string name")
      .WriteParameter("int age")
      .WriteParameterInterpolated(false, false, $"int test = {x}")
   .WriteEndParameterList()
   .WriteStartBaseList()
      .WriteBaseType("BaseClass")
      .WriteBaseType("IInterfaceBase", true)
   .WriteEndBaseList()
   .WriteStartGenericConstraints("T")
      .WriteGenericConstraint("notnull")
      .WriteGenericConstraint("IInterfaceName", true)
   .WriteLineEndGenericConstraints()
   .WriteStartGenericConstraints("T2")
      .WriteGenericConstraint("class")
   .WriteLineEndGenericConstraints()
   .OpenBody();

builder.WriteLine("public static void Test()")
   .OpenBody()
      .WriteLine("Console.WriteLine(\"Test\");")
   .CloseBody();

builder.TypeHeader
   .CloseBody();
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 138 9/8/2025
1.0.2 112 9/6/2025
1.0.0 111 9/6/2025