Lage.EnumDescription.Generator 2.1.1

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

Lage.EnumDescription.Generator

NuGet: Lage.EnumDescription.Generator · 仓库: Gitee / GitHub · 许可证: MIT-0

最后更新:2026-06-10

基于 Roslyn 增量生成器的零反射 · AOT 兼容枚举描述映射工具。编译时生成全部逻辑,安装即用。要求 .NET Core 3.0+ / .NET 5+。

使用

枚举模式

using Lage.EnumDescription.Core;

[LageDescriptionGenerate]
public enum OrderStatus
{
    [LageDescription("待支付")] Pending,
    [LageDescription("已支付")] Paid,
    [LageDescription("已发货")] Shipped,
}

// 编译后直接使用
OrderStatus.Paid.ToDescription();                          // "已支付"
OrderStatus.Paid.ToName();                                 // "Paid"
OrderStatusExtensions.ParseByName("Shipped");              // OrderStatus.Shipped
OrderStatusExtensions.TryParseByName("Paid", out var s);   // true, s = Paid
OrderStatusExtensions.TryParseByDescription("待支付", out var p); // true, p = Pending

常量类模式

[LageDescriptionGenerate]
internal partial class UserRole
{
    [LageDescription("普通用户")] public const string Normal = nameof(Normal);
    [LageDescription("管理员")]   public const string Admin  = nameof(Admin);
}

// 编译后直接使用
UserRole.ToDescription("Admin");                           // "管理员"
UserRole.TryParseByDescription("普通用户", out var r);     // true, r = "Normal"

嵌套类型(v2.0.1+)

枚举可放在外层类中,生成器自动处理包裹层级:

public partial class NestedContainer
{
    [LageDescriptionGenerate]
    public enum InnerStatus
    {
        [LageDescription("打开")] Open,
        [LageDescription("关闭")] Closed,
    }
}

NestedContainer.InnerStatus.Open.ToDescription();           // "打开"

常量类必须声明为 partial,否则编译器报告 LAGE001 错误。

API

方法 枚举模式 常量类模式
ToDescription 枚举值 → 描述 名称 → 描述
ToName 枚举值 → 名称 —
ParseByName 名称 → 枚举(抛异常) —
TryParseByName 名称 → 枚举(安全) —
TryParseByDescription 描述 → 枚举(安全) 描述 → 名称(安全)
GeneratedSource 完整查找表 完整查找表

完整文档

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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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
2.1.1 134 7/14/2026
2.1.0 121 6/10/2026
2.0.1 118 6/9/2026
2.0.0 145 6/8/2026