Fengb3.EasyCodeBuilder 0.1.1

dotnet add package Fengb3.EasyCodeBuilder --version 0.1.1
                    
NuGet\Install-Package Fengb3.EasyCodeBuilder -Version 0.1.1
                    
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="Fengb3.EasyCodeBuilder" Version="0.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Fengb3.EasyCodeBuilder" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Fengb3.EasyCodeBuilder" />
                    
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 Fengb3.EasyCodeBuilder --version 0.1.1
                    
#r "nuget: Fengb3.EasyCodeBuilder, 0.1.1"
                    
#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 Fengb3.EasyCodeBuilder@0.1.1
                    
#: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=Fengb3.EasyCodeBuilder&version=0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Fengb3.EasyCodeBuilder&version=0.1.1
                    
Install as a Cake Tool

Easy Code Builder

NuGet Version License: MIT CI NuGet

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:

  1. Create option objects - Each code construct has a corresponding option class
  2. Configure properties - Use With* methods or set properties directly
  3. Add children - Nest options within options to build complex structures
  4. 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 properties
  • Public, Private, Internal, Static - Keyword configurator properties for fluent keyword specification
  • KeywordConfigurator - Access the keyword configurator for advanced chaining with Parent property
  • Using() - Add using statements
  • Namespace(), Class(), Method() - Add structural elements
  • AutoProperty(), Constructor() - Add members
  • For(), While(), If(), Switch() - Add control flow
  • AppendLine() - Add raw code lines

Building Code

There are two main ways to build code:

  1. Direct building from options:

    var option = new TypeOption().WithName("MyClass");
    var code = option.Build();
    
  2. 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.


Made with ❤️ by Bohan Feng

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.

Version Downloads Last Updated
0.1.1 283 12/18/2025
0.1.0 280 12/17/2025
0.0.6 233 10/23/2025
0.0.5 191 10/23/2025
0.0.4 187 7/29/2025
0.0.3 137 7/28/2025
0.0.2 127 6/21/2025
0.0.1 116 6/21/2025

GitHub Actions CI/CD自动化发布测试版本