Lage.AutoBuilder 1.1.0

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

Lage.AutoBuilder

NuGet License

🔧 基于 Roslyn 增量源生成器(IIncrementalGenerator)的编译期 Builder 模式自动代码生成器。

只需一个 [Buildable] 特性,自动为你的类生成 Builder 类,告别手写繁琐的建造者代码。


✨ 特性

特性 说明
[Buildable] 标记目标类,自动生成 XxxBuilder
[BuildIgnore] 跳过某个属性,不出现在 Builder 中
[BuildMethod] 指定静态工厂方法作为构建入口
CreateBuilder() 目标类自动生成静态入口方法
global:: 全限定名 生成代码全部使用全限定名,零冲突
作用域一致 Builder 可访问性与主类保持一致

📦 安装

dotnet add package Lage.AutoBuilder

编译要求:.NET 6.0 SDK 及以上 | 生成的代码兼容 .NET 5.0+ 运行时


🚀 快速开始

1. 标记类 + 属性

using Lage.AutoBuilder.Core.Attributes;

[Buildable]
public partial class Person
{
    public string Name { get; set; } = null!;
    public string Password { get; set; } = null!;

    [BuildIgnore]                      // 该属性不出现在 Builder 中
    public string InternalId { get; set; } = null!;

    public int Age { get; set; }
    public string? Email { get; set; }
}

2. 使用生成的 Builder

var person = Person.CreateBuilder()
    .WithName("张三")
    .WithPassword("abc123456")
    .WithAge(28)
    .WithEmail("zhangsan@example.com")
    .Build();

3. 静态工厂方法 [BuildMethod]

[Buildable]
public partial class Order
{
    public string Id { get; }
    public decimal Amount { get; }
    public DateTime CreatedAt { get; }

    private Order(string id, decimal amount, DateTime createdAt)
    {
        Id = id;
        Amount = amount;
        CreatedAt = createdAt;
    }

    [BuildMethod]
    public static Order Create(string id, decimal amount, DateTime createdAt)
        => new(id, amount, createdAt);
}

// 生成:
var order = Order.CreateBuilder()
    .WithId("ORD-001")
    .WithAmount(99.99m)
    .WithCreatedAt(DateTime.Now)
    .Build();

🏗️ 项目结构

Lage.AutoBuilder/
├── src/
│   ├── Lage.AutoBuilder.Core/         # 特性定义(netstandard2.0)
│   │   └── Attributes/
│   │       ├── BuildableAttribute.cs
│   │       ├── BuildIgnoreAttribute.cs
│   │       └── BuildMethodAttribute.cs
│   ├── Lage.AutoBuilder.Generator/    # 源生成器(netstandard2.0)
│   │   ├── Generator.cs               # IIncrementalGenerator 入口
│   │   ├── ClassInfo.cs               # 数据模型
│   │   ├── PropertyInfo.cs            # 属性元数据
│   │   ├── MethodInfo.cs              # 工厂方法元数据
│   │   ├── ValidationInfo.cs          # 验证规则 + Collector(预留)
│   │   ├── StringBuilderExtensions.cs # 缩进统一管理
│   │   └── IsExternalInit.cs          # netstandard2.0 polyfill
│   └── Lage.AutoBuilder/              # NuGet 打包项目
├── test/
│   └── ConsoleTest/                   # 集成测试
└── README.md

🔧 技术栈

  • Roslyn Source Generators (IIncrementalGenerator)
  • netstandard2.0 → 兼容 .NET 6.0 / 7.0 / 8.0 / 9.0 / 10.0
  • 编译期零反射、零运行时开销
  • ForAttributeWithMetadataName 增量管线

📝 更新日志

v1.1.0

  • ✅ CreateBuilder() 入口(替代 new XxxBuilder())
  • ✅ [BuildIgnore] 跳过属性
  • ✅ [BuildMethod] 工厂方法模式
  • ✅ 全量 global:: 命名空间 + 作用域一致
  • ✅ ValidationInfo.Collector 流式 API

v1.0.0

  • ✅ [Buildable] 属性复制模式

📄 License

MIT © LageYang

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
1.1.0 120 7/21/2026