https://github.com/h-shahzaib/flynth
A modern, easy-to-use, and readable alternative to legacy text templating and source code generation libraries, featuring a C# based fluent API.
https://github.com/h-shahzaib/flynth
c-sharp code csharp easy fluent gen generator library modern sharp source
Last synced: 4 months ago
JSON representation
A modern, easy-to-use, and readable alternative to legacy text templating and source code generation libraries, featuring a C# based fluent API.
- Host: GitHub
- URL: https://github.com/h-shahzaib/flynth
- Owner: h-shahzaib
- License: mit
- Created: 2025-02-05T12:38:58.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2025-06-26T09:58:56.000Z (4 months ago)
- Last Synced: 2025-06-26T10:36:32.675Z (4 months ago)
- Topics: c-sharp, code, csharp, easy, fluent, gen, generator, library, modern, sharp, source
- Language: C#
- Homepage:
- Size: 78.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Flynth.CSharp
> **Flynth.CSharp** is a fluent, zero-dependency source code generator for C#.
> It helps you create structured C# source code through a clean, builder-style API โ so you can focus on _what_ to generate, not _how_ to format it.---
## ๐ฆ Installation
Install via the .NET CLI:
```
dotnet add package Flynth.CSharp
```---
## ๐ Overview
Built on **.NET Standard 2.0**, Flynth.CSharp works across **all .NET versions** with **no external dependencies**.
It handles indentation, structure, formatting, and character escaping โ all out of the box.---
## โจ Key Features
### โ Fluent API โ Code That Writes Code
Write source generation logic that looks nearly identical to the output.
No manual formatting, indentation, or brace management required.### ๐ง Token-Aware Formatting
The builder system knows its context โ it places line breaks and indentation only where needed, making your generated code clean and readable.
### ๐ Character Replacement System
Write code with backticks instead of escaped double quotes:
```csharp
_method.Line("Console.WriteLine(`Hello World!`);");
```You can customize or remove replacements:
```csharp
_root.ChildOptions.RegisterCharReplacement('`', '"');
_root.ChildOptions.RemoveCharReplacement('`');
```---
## ๐งช Example
This C# code:
```csharp
using System.IO;
using System.Collections.Generic;namespace MyNamespace
{
public class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine("Hello World!");var employees = await m_DbContext.Employees
.Where(i => i.Age >= 30)
.ToListAsync();Console.WriteLine($"Total employees count: {employees.Count}");
}
}
}
```Can be generated like this:
```csharp
using Flynth.CSharp;var _root = new SourceBuilder();
_root.Using("System.IO");
_root.Using("System.Collections.Generic");_root.Namespace("MyNamespace", _namespace =>
{
_namespace.Class("public", "Program", _class =>
{
_class.Method("public static async", "Task", "Main", "string[] args", _method =>
{
_method.Line("Console.WriteLine(`Hello World!`);");
_method.Line("Console.WriteLine(`Hello World!`);");_method.Lines(
"var employees = await m_DbContext.Employees",
" .Where(i => i.Age >= 30)",
" .ToListAsync();"
);_method.Line("Console.WriteLine($`Total employees count: {employees.Count}`);");
});
});
});var result = _root.ToString();
```---
## โ ๏ธ Compatibility Note
Flynth works with all .NET targets that support **.NET Standard 2.0**.
If you're using older .NET versions like .NET Core < 3.0 or .NET Framework, be aware of this:### ๐ Variable Name Conflicts in Nested Scopes
Older compilers don't allow reuse of the same variable name inside nested lambdas:
```csharp
_class.Method("public", "void", "Main", "", _method =>
{
_method.Line("var number = 10;");_method.Method("", "void", "InnerMethod", "", _method => // โ Conflict
{
_method.Line("var number = 20;");
});
});
```#### โ Solution:
Use a different name in inner scopes (e.g. `_inner_method`).
---
## ๐ค Why Flynth?
- โ Fluent, readable, and chainable API
- โ No need to manage syntax or formatting manually
- โ Output closely mirrors your generation code
- โ No dependencies
- โ Works with any .NET project---
## ๐ Getting Started
1. Install the package
2. Use `SourceBuilder` to define your structure
3. Call `.ToString()` to get the generated code---
## ๐ Links
- ๐ฆ NuGet: [https://www.nuget.org/packages/Flynth.CSharp/](https://www.nuget.org/packages/Flynth.CSharp/)
- ๐ป GitHub: [https://github.com/h-shahzaib/Flynth](https://github.com/h-shahzaib/Flynth)