Fengb3.EasyCodeBuilder
0.1.1
dotnet add package Fengb3.EasyCodeBuilder --version 0.1.1
NuGet\Install-Package Fengb3.EasyCodeBuilder -Version 0.1.1
<PackageReference Include="Fengb3.EasyCodeBuilder" Version="0.1.1" />
<PackageVersion Include="Fengb3.EasyCodeBuilder" Version="0.1.1" />
<PackageReference Include="Fengb3.EasyCodeBuilder" />
paket add Fengb3.EasyCodeBuilder --version 0.1.1
#r "nuget: Fengb3.EasyCodeBuilder, 0.1.1"
#:package Fengb3.EasyCodeBuilder@0.1.1
#addin nuget:?package=Fengb3.EasyCodeBuilder&version=0.1.1
#tool nuget:?package=Fengb3.EasyCodeBuilder&version=0.1.1
Easy Code Builder
A powerful and flexible library for dynamic C# code generation using a configuration-based API.
✨ Features
- ⚙️ Configuration-Based API: Use option objects to configure code structures
- 🏗️ Type-Safe Builder: Strongly typed options for C# code generation
- 🔧 Flexible Configuration: Declarative approach with extension methods
- 📚 Rich Code Constructs: Namespaces, classes, methods, properties, control structures, and more
- ⚡ High Performance: Optimized string building with efficient rendering
- 🛡️ Type Safety: Compile-time safety with strongly typed options
- 📖 XML Documentation: Complete IntelliSense support
🚀 Quick Start
Installation
dotnet add package Fengb3.EasyCodeBuilder
Basic Usage
<div style="display: flex; gap: 20px;"> <div style="flex: 1;">
API Calling:
using Fengb3.EasyCodeBuilder.Csharp;
using static Fengb3.EasyCodeBuilder.Csharp.Code;
var code = Create()
.Using("System")
.Namespace(ns => {
ns.Name = "MyProject";
ns.Public.Class(cls => {
cls.WithName("Person");
cls.Public.AutoProperty(p => p
.WithType("string")
.WithName("Name")
);
cls.Public.AutoProperty(p => p
.WithType("int")
.WithName("Age")
);
});
})
.Build();
</div> <div style="flex: 1;">
Generated Code:
using System;
namespace MyProject
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
</div> </div>
📚 Documentation
For detailed documentation and examples, see this README and the test project examples in the repository.
🏗️ Architecture
EasyCodeBuilder v0.1.0 uses a configuration-based approach with option objects:
| Component | Description |
|---|---|
CodeOption |
Base class for all code options |
Code.Create() |
Entry point for creating code |
NamespaceOption |
Configure namespaces |
TypeOption |
Configure classes, structs, interfaces |
MethodOption |
Configure methods |
AutoPropertyOption |
Configure auto-properties |
IfOption/ElseIfOption/ElseOption |
Configure conditional statements |
ForOption/WhileOption/ForeachOption |
Configure loops |
SwitchOption |
Configure switch statements |
📊 Examples
Generate a Class with Methods
<div style="display: flex; gap: 20px;"> <div style="flex: 1;">
API Calling:
using static Fengb3.EasyCodeBuilder.Csharp.Code;
var code = Create()
.Using("System")
.Namespace(ns => {
ns.Name = "MyApp";
ns.Public.Class(cls => {
cls.WithName("Calculator");
cls.Public.Method(method => {
method.WithName("Add")
.WithReturnType("int")
.WithParameters("int a", "int b")
.AppendLine("return a + b;");
});
});
})
.Build();
</div> <div style="flex: 1;">
Generated Code:
using System;
namespace MyApp
{
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}
}
</div> </div>
Using Keyword Configurator
The new KeywordOptionConfigurator API provides a fluent way to specify access modifiers and other keywords. Note: You need to import Fengb3.EasyCodeBuilder.Csharp.OptionConfigurations to use these extension methods.
<div style="display: flex; gap: 20px;"> <div style="flex: 1;">
API Calling:
using Fengb3.EasyCodeBuilder.Csharp;
using Fengb3.EasyCodeBuilder.Csharp.OptionConfigurations;
var @namespace = new NamespaceOption()
.WithName("MyNamespace")
.Public.Class(cls => {
cls.WithName("MyClass");
});
var code = @namespace.Build();
</div> <div style="flex: 1;">
Generated Code:
namespace MyNamespace
{
public class MyClass
{
}
}
</div> </div>
You can also use the keyword configurator with auto-properties:
<div style="display: flex; gap: 20px;"> <div style="flex: 1;">
API Calling:
using Fengb3.EasyCodeBuilder.Csharp;
using static Fengb3.EasyCodeBuilder.Csharp.Code;
var @class = Create()
.Class(cls => cls
.WithName("MyClass")
.Public.AutoProperty(prop => prop
.WithName("MyProperty")
.WithType(typeof(int).FullName ?? "int")
)
);
var classCode = @class.Build();
</div> <div style="flex: 1;">
Generated Code:
class MyClass
{
public System.Int32 MyProperty { get; set; }
}
</div> </div>
Using Constructors
<div style="display: flex; gap: 20px;"> <div style="flex: 1;">
API Calling:
using Fengb3.EasyCodeBuilder.Csharp;
using Fengb3.EasyCodeBuilder.Csharp.OptionConfigurations;
// Create a public class using KeywordConfigurator approach
var classOption = new TypeOption()
.WithTypeKind(TypeOption.Type.Class)
.WithName("Person")
.WithKeyword("public"); // TypeOption's public keyword
// Add constructor (note: Constructor doesn't support KeywordConfigurator yet)
classOption.Constructor(ctor => {
ctor.WithKeyword("public")
.WithParameter("string name")
.WithParameter("int age")
.AppendLine("Name = name;")
.AppendLine("Age = age;");
});
// Use KeywordConfigurator for properties
classOption.Public.AutoProperty(p => p
.WithType("string")
.WithName("Name"));
classOption.Public.AutoProperty(p => p
.WithType("int")
.WithName("Age"));
var code = classOption.Build();
</div> <div style="flex: 1;">
Generated Code:
public class Person
{
public Person(string name, int age)
{
Name = name;
Age = age;
}
public string Name { get; set; }
public int Age { get; set; }
}
</div> </div>
Control Structures - For Loop
<div style="display: flex; gap: 20px;"> <div style="flex: 1;">
API Calling:
using Fengb3.EasyCodeBuilder.Csharp;
using Fengb3.EasyCodeBuilder.Csharp.OptionConfigurations;
// Create a method using KeywordConfigurator for public access
var classOption = new TypeOption()
.WithTypeKind(TypeOption.Type.Class)
.WithName("NumberPrinter");
classOption.Public.Method(method => {
method.WithName("PrintNumbers")
.WithReturnType("void")
.For(@for => {
@for.WithInitializer("int i = 0")
.WithCondition("i < 10")
.WithIterator("i++")
.AppendLine("Console.WriteLine(i);");
});
});
var code = classOption.Build();
</div> <div style="flex: 1;">
Generated Code:
class NumberPrinter
{
public void PrintNumbers()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
}
}
</div> </div>
Control Structures - Switch Statement
<div style="display: flex; gap: 20px;"> <div style="flex: 1;">
API Calling:
using Fengb3.EasyCodeBuilder.Csharp;
using Fengb3.EasyCodeBuilder.Csharp.OptionConfigurations;
// Create a class with a public method using KeywordConfigurator
var classOption = new TypeOption()
.WithTypeKind(TypeOption.Type.Class)
.WithName("DayHelper");
classOption.Public.Method(method => {
method.WithName("GetDayName")
.WithReturnType("string")
.WithParameters("int day"); // WithParameters for methods
method.Switch(@switch => {
@switch.Expression = "day";
@switch.Case(@case => {
@case.Value = "1";
@case.AppendLine("return \"Monday\";");
});
@switch.Case(@case => {
@case.Value = "2";
@case.AppendLine("return \"Tuesday\";");
});
@switch.Default(@default =>
@default.AppendLine("return \"Unknown\";")
);
});
});
var code = classOption.Build();
</div> <div style="flex: 1;">
Generated Code:
class DayHelper
{
public string GetDayName(int day)
{
switch (day)
{
case 1:
{
return "Monday";
}
case 2:
{
return "Tuesday";
}
default:
{
return "Unknown";
}
}
}
}
</div> </div>
🎯 Key Concepts
Option-Based Configuration
EasyCodeBuilder v0.1.0 uses a configuration-based approach where you:
- Create option objects - Each code construct has a corresponding option class
- Configure properties - Use
With*methods or set properties directly - Add children - Nest options within options to build complex structures
- Build the code - Call
.Build()to generate the final code string
Extension Methods
The library provides fluent extension methods for common operations:
WithName(),WithKeyword(),WithType()- Configure basic propertiesPublic,Private,Internal,Static- Keyword configurator properties for fluent keyword specificationKeywordConfigurator- Access the keyword configurator for advanced chaining withParentpropertyUsing()- Add using statementsNamespace(),Class(),Method()- Add structural elementsAutoProperty(),Constructor()- Add membersFor(),While(),If(),Switch()- Add control flowAppendLine()- Add raw code lines
Building Code
There are two main ways to build code:
Direct building from options:
var option = new TypeOption().WithName("MyClass"); var code = option.Build();Using the Code.Create() entry point:
var code = Code.Create() .Using("System") .Namespace(ns => { ... }) .Build();
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🔗 Links
- NuGet Package: Fengb3.EasyCodeBuilder
- GitHub Repository: https://github.com/fengb3/EasyCodeBuilder
- Issues: Report a bug or request a feature
Made with ❤️ by Bohan Feng
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
GitHub Actions CI/CD自动化发布测试版本