credfeto/credfeto-enum-source-generation

GitHub: credfeto/credfeto-enum-source-generation

C# Roslyn 源代码生成器,在编译期为枚举自动生成高性能扩展方法,实现零内存分配的 GetName、GetDescription 和 IsDefined 操作。

Stars: 20 | Forks: 1

# credfeto-enum-source-generation C# 枚举源生成器 ## 使用说明 在需要进行代码生成的每个项目中添加对 ``Credfeto.Enumeration.Source.Generation`` 包的引用。 ``` ``` 对于项目中的每个枚举,会生成一个包含以下扩展方法的类: * public static string GetName(this MyEnum value) * public static string GetDescription(this MyEnum value) 假设定义了如下示例枚举: ``` public enum ExampleEnumValues { ZERO = 0, [Description("One \"1\"")] ONE = 1, SAME_AS_ONE = ONE, } ``` 获取枚举值的名称和值。在 Release 模式下,这几乎是瞬间完成的。 ``` ExampleEnumValues value = ExampleEnumValues.ONE; string name = value.GetName(); // ONE string description = value.GetDescription(); // One "1" bool isDefined = value.IsDefine(); // true bool isNotDefined = ((ExampleEnumValues)42).IsDefine(); // false ``` ## 其他程序集中的枚举 在包含要生成的枚举扩展类的项目中引用以下包。 ``` ``` 为你想要公开的每个枚举,向 partial static 扩展类添加 ``EnumText`` 属性。 ``` [EnumText(typeof(System.Net.HttpStatusCode))] [EnumText(typeof(ThirdParty.ExampleEnum))] public static partial class EnumExtensions { } ``` 这将生成相同的扩展方法,但针对属性中指定的类型。 ## 基准测试 基准测试位于 Benchmark.net 项目 ``Credfeto.Enumeration.Source.Generation.Benchmarks`` 中,以下是运行摘要。 | Method | Mean | Error | StdDev | Median | Allocated | |------------------------------- |--------------:|-----------:|-----------:|--------------:|----------:| | GetNameToString | 25.5162 ns | 0.4146 ns | 0.3675 ns | 25.5322 ns | 24 B | | GetNameReflection | 37.8875 ns | 0.3971 ns | 0.3520 ns | 37.8542 ns | 24 B | | GetNameCachedReflection | 21.6571 ns | 0.4514 ns | 0.3770 ns | 21.6842 ns | 24 B | | GetNameCodeGenerated | 0.0009 ns | 0.0039 ns | 0.0036 ns | 0.0000 ns | - | | GetDescriptionReflection | 1,380.4979 ns | 15.1089 ns | 13.3937 ns | 1,382.9476 ns | 264 B | | GetDescriptionCachedReflection | 22.8844 ns | 0.3856 ns | 0.3607 ns | 22.8364 ns | 24 B | | GetDescriptionCodeGenerated | 0.0035 ns | 0.0057 ns | 0.0053 ns | 0.0000 ns | - | | IsDefinedCodeReflection | 48.7961 ns | 0.9675 ns | 1.0352 ns | 48.5573 ns | 24 B | | IsDefinedCodeReflectionCached | 21.4452 ns | 0.3169 ns | 0.2965 ns | 21.3938 ns | 24 B | | IsDefinedCodeGenerated | 0.0012 ns | 0.0041 ns | 0.0037 ns | 0.0000 ns | - | ``` // * Warnings * ZeroMeasurement EnumBench.GetNameCodeGenerated: Default -> The method duration is indistinguishable from the empty method duration EnumBench.GetDescriptionCodeGenerated: Default -> The method duration is indistinguishable from the empty method duration EnumBench.IsDefinedCodeGenerated: Default -> The method duration is indistinguishable from the empty method duration // * Legends * Mean : Arithmetic mean of all measurements Error : Half of 99.9% confidence interval StdDev : Standard deviation of all measurements Median : Value separating the higher half of all measurements (50th percentile) Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B) 1 ns : 1 Nanosecond (0.000000001 sec) ``` ## 查看编译器生成的文件 在 csproj 文件中添加以下内容: ``` true Generated ``` ## 构建状态 | Branch | Status | |---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | main | [![Build: Pre-Release](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/1fec513b66220738.svg)](https://github.com/credfeto/credfeto-enum-source-generation/actions/workflows/build-and-publish-pre-release.yml) | | release | [![Build: Release](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/b15f328d2a220741.svg)](https://github.com/credfeto/credfeto-enum-source-generation/actions/workflows/build-and-publish-release.yml) | ## 更新日志 查看 [更新日志](CHANGELOG.md)
标签:Enum, Roslyn, SOC Prime, Source Generator, 代码生成, 多人体追踪, 开发工具, 性能优化, 扩展方法, 枚举, 检测绕过, 渗透测试工具, 编程库, 编译时, 自动生成