A powerful and flexible library for dynamic code generation, supporting multiple programming languages including C#, Python, and Lisp.
- π Fluent API: Easy-to-use method chaining for intuitive code building
- π― Multi-Language Support: Generate C#, Python, and Lisp code
- π§ Configurable Indentation: Support for spaces, tabs, and custom indentation
- π Rich Code Constructs: Classes, methods, properties, control structures, and more
- β‘ High Performance: Optimized string building with caching mechanisms
- π‘οΈ Type Safety: Generic-based design ensures compile-time safety
- π XML Documentation: Complete IntelliSense support
dotnet add package Fengb3.EasyCodeBuilderAPI Calling:
using Fengb3.EasyCodeBuilder.Csharp;
var builder = new CSharpCodeBuilder();
var code = builder
.Using("System", "System.Collections.Generic")
.Namespace("MyProject", ns => ns
.Class("Person", cls => cls
.Property("string", "Name", accessors: "{ get; set; }")
.Property("int", "Age", accessors: "{ get; set; }")
.Method("GetInfo", method => method
.AppendLine("return $\"Name: {Name}, Age: {Age}\";")
, returnType: "string")
)
)
.ToString();Generated Code:
using System;
using System.Collections.Generic;
namespace MyProject
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string GetInfo()
{
return $"Name: {Name}, Age: {Age}";
}
}
}For detailed documentation and examples, please refer to:
- C# Code Builder Guide - Complete guide for C# code generation
- Python Code Builder Guide - Complete guide for Python code generation
| Language | Status | Builder Class |
|---|---|---|
| C# | β Full Support | CSharpCodeBuilder |
| Python | β Full Support | PythonCodeBuilder |
| Lisp | β Basic Support | LispCodeBuilder |
CodeBuilder<T>- Abstract base class with core functionalityCSharpCodeBuilder- C# specific code generationPythonCodeBuilder- Python specific code generationLispCodeBuilder- Lisp specific code generation
- Fluent API - Method chaining for readable code
- Indentation Management - Automatic and configurable indentation
- Code Blocks - Structured code generation with proper nesting
- Performance Optimized - Efficient string building and caching
API Calling:
var code = new CSharpCodeBuilder()
.Using("System")
.Namespace("MyApp.Models", ns => ns
.Class("User", cls => cls
.Field("string", "_name", "private")
.Property("string", "Name",
modifiers: "public",
accessors: "{ get => _name; set => _name = value ?? throw new ArgumentNullException(); }")
.Method("ToString", method => method
.AppendLine("return $\"User: {Name}\";")
, returnType: "string", modifiers: "public override")
)
);Generated Code:
using System;
namespace MyApp.Models
{
public class User
{
private string _name;
public string Name
{
get => _name;
set => _name = value ?? throw new ArgumentNullException();
}
public override string ToString()
{
return $"User: {Name}";
}
}
}API Calling:
var pythonCode = new PythonCodeBuilder()
.Import("json", "datetime")
.AppendLine()
.Class("User", cls => cls
.Function("__init__", init => init
.AppendLine("self.name = name")
.AppendLine("self.created_at = datetime.datetime.now()")
, "self, name: str")
.AppendLine()
.Function("to_dict", func => func
.AppendLine("return {")
.AppendLine(" 'name': self.name,")
.AppendLine(" 'created_at': self.created_at.isoformat()")
.AppendLine("}")
, "self", "dict")
);Generated Code:
import json
import datetime
class User:
def __init__(self, name: str):
self.name = name
self.created_at = datetime.datetime.now()
def to_dict(self) -> dict:
return {
'name': self.name,
'created_at': self.created_at.isoformat()
}API Calling:
var code = new CSharpCodeBuilder()
.Namespace("Examples", ns => ns
.Class("Calculator", cls => cls
.Method("Divide", method => method
.If("b == 0", ifBlock => ifBlock
.AppendLine("throw new DivideByZeroException(\"Cannot divide by zero\");")
)
.AppendLine("return a / b;")
, returnType: "double", parameters: "double a, double b")
)
);Generated Code:
namespace Examples
{
public class Calculator
{
public double Divide(double a, double b)
{
if (b == 0)
{
throw new DivideByZeroException("Cannot divide by zero");
}
return a / b;
}
}
}Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- NuGet Package: Fengb3.EasyCodeBuilder
- GitHub Repository: https://github.com/fengb3/EasyCodeBuilder
- Issues: Report a bug or request a feature
Made with β€οΈ by Bohan Feng