Hysen Labs
Open-source project
credfeto/credfeto-enum-source-generation avatar
credfeto

credfeto-enum-source-generation

C# source generator for producing faster enum lookups

22 stars1 forksC#MIT
DEEP OPEN-SOURCE ANALYSIS

credfeto-enum-source-generation: a C# source generator for enums

This package generates extension methods for C# enums at compile time so name and description lookups run without reflection or dictionaries.

What the source generator produces

credfeto-enum-source-generation is a C# source generator for enums. The README describes it as a source generator that runs during compilation to produce code for each enum in a project. To use it, a developer adds a reference to the Credfeto.Enumeration.Source.Generation package in every project where they want the generation to happen. After that, for each enum the project defines, the generator emits a class containing extension methods. The README names two methods in particular: a public static string GetName method that takes the enum value and returns its name, and a public static string GetDescription method that returns the value's description. These are generated as ordinary code, so at runtime they are plain method calls rather than reflection based lookups. The point of generating source instead of writing the helpers by hand is to avoid repetitive boilerplate across many enums while keeping the result fast. Because the methods are real code in the assembly, the compiler can inline and optimize them. The README frames the tool as a way to get the name and description of an enum value quickly, and notes that in release mode the result can be practically instant, which matters for code paths that convert enums to text frequently.

How the generated methods behave

The README shows a worked example using an enum called ExampleEnumValues with members ZERO equal to 0, ONE equal to 1 with a Description attribute reading One and a quote, and SAME_AS_ONE aliased to ONE. With the generated methods, calling value.GetName on ExampleEnumValues.ONE returns the string ONE, and calling value.GetDescription returns the string One followed by a quoted 1. The generator also produces an IsDefine method, and the README demonstrates that ((ExampleEnumValues)42).IsDefine returns false because 42 is not a defined member, while a valid value returns true. This lets calling code check whether a raw integer maps to a real enum member before using it, which is useful when parsing external input. The description comes from the System.ComponentModel.Description attribute placed on each member, so the text shown to users can differ from the member name used in code. Because the methods are generated per enum, there is no central dictionary to keep in sync and no runtime cost for attribute scanning. The README keeps the example small, but the mechanism applies to any enum in the referenced project without extra authoring once the package is referenced. The IsDefine check is the part that protects callers from passing an undefined integer into logic that assumes a valid member exists.

Generating methods for enums in other assemblies

The README explains a second mode for enums that live in a different assembly from the one doing the lookups. A project that wants extension methods for those external enums references a second package, Credfeto.Enumeration.Source.Generation.Attributes, shown in the README at version 0.0.2.3. The developer then adds an EnumText attribute to a partial static class, listing the types they want to expose. The example uses attributes targeting System.Net.HttpStatusCode and a third party example enum called ThirdParty.ExampleEnum. With those attributes in place, the generator emits the same GetName, GetDescription, and IsDefine methods, but for the types named in the attributes rather than for enums declared in the current project. This is helpful when an application consumes a library and wants fast name and description text for that library's enums without modifying the library itself. The README shows the attribute usage as a partial static class so the generated part can be combined with any hand written members the developer adds. Both packages use PrivateAssets set to All and ExcludeAssets set to runtime, which keeps the generator and its attributes out of the runtime dependency set of the consuming project. That setting matters because a source generator should not appear as a normal library reference shipped in the final build output.

Benchmarks and intended use

The README mentions that benchmarks live in a Benchmark project and references BenchmarkDotNet style measurement, which is the usual way C# developers compare the speed of different implementations. The intent behind publishing benchmarks is to show that generated, compile time methods beat reflection or dictionary based approaches for enum to text conversion. Because the generated methods are plain code, the just in time compiler can optimize them, and the README states that in release mode the lookups can be practically instant. That claim is what the benchmarks are meant to support. The tool is aimed at codebases with many enums or with hot paths that convert enum values to display strings, such as logging, API responses, or user interfaces. The MIT license lets the package be used in proprietary and open source projects without copyleft obligations. The two package references, one for generating from local enums and one with attributes for external enums, cover the common cases a team meets. The README is short and focused, giving the install snippet, a code example, and the cross assembly pattern rather than a long feature list, which fits a small single purpose utility. A team evaluating it can rely on the stated release mode performance and the benchmark project to confirm the speed claims for its own enums.

Editorial conclusion

The generator is written in C sharp and published under the MIT license. The README shows a package reference at version 1.0.0.11 and states lookups can be practically instant in release mode.

DEEP OPEN-SOURCE ANALYSIS

Official sources

Community notes

Community notes