# Credfeto Enum Source Generation: Faster Enum Lookups Without Reflection

> A C# source generator that replaces reflection-based enum name, description, and defined checks with compile-time generated code. It targets developers who need fast, allocation-free enum operations in hot paths.

**credfeto/credfeto-enum-source-generation** — C# source generator for producing faster enum lookups

- Repository: https://github.com/credfeto/credfeto-enum-source-generation
- Stars: 23 · Forks: 1
- Language: C#
- License: MIT
- Published: 2026-08-23 · Updated: 2026-08-23 · Language: en
- Canonical page: https://hysenlabs.com/projects/credfeto-credfeto-enum-source-generation

## What Problem This Solves

The core problem is that standard .NET enum operations, such as ToString(), Enum.GetName(), and Enum.IsDefined(), rely on reflection. Reflection is slow and allocates memory, which hurts in performance-sensitive code like logging, serialization, or command parsing. This project generates compile-time code that performs the same lookups without reflection. It is aimed at C# developers who have identified enum operations as a bottleneck and want a drop-in replacement that does not require changing how enums are declared. The README positions it as a way to get names and descriptions 'practically instant' in release mode, which is a direct claim about the generated code's speed.

## How the Generator Works

The generator scans every enum in the project and produces a static class with extension methods: GetName(), GetDescription(), and IsDefine(). Note the method is IsDefine, not IsDefined, which is a minor naming quirk you should be aware of. The generated code likely uses a switch expression or a dictionary lookup, but the README does not specify the exact mechanism. What is clear is that the generated methods are so fast that BenchmarkDotNet reports their duration as indistinguishable from an empty method. The benchmark table shows GetNameCodeGenerated at 0.0009 ns mean with zero allocation, versus 25.5 ns for ToString() and 37.9 ns for reflection. This indicates the generated code is essentially a compile-time constant lookup, which is only possible because the enum values are known at build time.

## Getting It Running

To use the generator, add a PackageReference to Credfeto.Enumeration.Source.Generation in each project that needs the code generation. The README shows version 1.0.0.11, but the latest release is v1.2.151.2192, so you should use a recent version. The package reference must include PrivateAssets="All" and ExcludeAssets="runtime" to keep it as a build-time dependency. After adding the package, the generator automatically creates extension methods for every enum in the project. For enums defined in other assemblies, you need a second package, Credfeto.Enumeration.Source.Generation.Attributes, and you must declare a partial static class with an [EnumText] attribute for each external enum type. For example, [EnumText(typeof(System.Net.HttpStatusCode))] on a partial class generates the extension methods for that type. The README also shows how to emit generated files for inspection by setting EmitCompilerGeneratedFiles to true and specifying an output path.

## Benchmarks and Their Caveats

The benchmark results are striking, but they come with a warning. BenchmarkDotNet flags the generated methods with 'ZeroMeasurement', meaning their duration is indistinguishable from an empty method. That is not a failure; it is the expected outcome for code that compiles to a constant or a simple switch. The comparison is against ToString(), reflection, and cached reflection. GetNameCodeGenerated is 0.0009 ns, while GetNameReflection is 37.9 ns, a factor of over 40,000. GetDescriptionCodeGenerated is 0.0035 ns versus 1,380 ns for uncached reflection. The allocation column shows zero bytes for generated methods, while reflection allocates 24 B or more. These numbers are from a single run in the repository, not from independent testing, so you should treat them as indicative rather than guaranteed. The real takeaway is that generated code eliminates both time and allocation, which is the intended design.

## Limitations and Wrong Tool Cases

The most obvious limitation is that the generator only works for enums known at compile time. If your enum values come from a database or a configuration file, this tool cannot help. Also, the generated methods are fixed: if you add a new enum member, you must recompile. That is fine for most applications, but it rules out plugins or dynamic code loading. Another limitation is the IsDefine method name. It is a typo of IsDefined, and while it is consistent, it will look odd in code reviews. The README does not mention any configuration options, so you cannot customize the generated method names or add additional attributes. The generator also only produces GetName, GetDescription, and IsDefine. It does not generate parsing methods, such as Enum.TryParse equivalents, which are often the slowest enum operations. If you need fast parsing, this project is not the answer.

## Comparing to Alternatives

The standard alternative is to use a precomputed dictionary, either manually or via a library like Enums.NET. Enums.NET provides a comprehensive set of enum utilities, including parsing, with caching to reduce reflection overhead. The difference in approach is that Enums.NET uses runtime caching and reflection on first access, while this generator bakes the lookup into the assembly at compile time. That means Enums.NET can handle dynamically loaded enums, but it still has a first-call cost and may allocate on cache misses. Another alternative is to write a switch statement by hand for each enum, which gives the same speed but requires manual maintenance. The generator automates that manual work, which is its main advantage. The trade-off is that you lose the flexibility of a runtime library.

## Maintenance and Upgrade Cost

The project is under active development, with releases on 2026-07-15, 2026-07-11, and 2026-06-29. That means you should expect regular updates, which is good for bug fixes but adds upgrade overhead. The README references a changelog, so you can review breaking changes before updating. The package is marked as a source generator, so it does not ship runtime assemblies, which keeps your deployment small. The license is MIT, which is permissive and allows commercial use without restrictions, but you should still check the changelog for any changes to the generated output that could affect your build. Since the generator runs at compile time, upgrading the package is as simple as changing the version number in your csproj, but you should re-run your tests because the generated code may change.

## Conclusion

Adopt this project if your application spends measurable time on enum.ToString(), Enum.GetName(), or Enum.IsDefined() in loops or serialization paths, and you can accept a compile-time dependency. Avoid it if you need dynamic behavior at runtime, such as adding enum values without recompiling, because the generated code is fixed at build time. Before adopting, verify that the generated extension methods cover all enums you need, especially those in referenced assemblies, and confirm the package version in your project matches the latest release. Check the generated code for your specific enums to ensure the description attribute handling matches your expectations, since the README shows only a simple example with a quoted string. The MIT license and active release cadence (three releases in a month) suggest low licensing risk, but you should review the changelog for breaking changes before upgrading.

## FAQ

### How do I add credfeto-enum-source-generation to a C# project?

Add a PackageReference to Credfeto.Enumeration.Source.Generation in each project where generation should run, with PrivateAssets set to All and ExcludeAssets set to runtime so the package never reaches the deployed output. The generator then emits GetName and GetDescription extension methods for every enum in that project.

### How do I use credfeto-enum-source-generation with enums defined in another assembly?

Reference Credfeto.Enumeration.Source.Generation.Attributes in the project that declares the extension class, then put an EnumText attribute on a public static partial class for each enum type you want to expose. The generator emits the same extension methods for the types named in the attributes.

### Does credfeto-enum-source-generation speed up enum lookups, and what do its benchmarks show?

The table reports the generated methods at 0.0009, 0.0035, and 0.0012 nanoseconds with no allocated bytes, but BenchmarkDotNet also raises a ZeroMeasurement warning for all three, saying the duration is indistinguishable from the empty method. The reflection baseline for GetDescription is 1,380.4979 ns with 264 B allocated.

## Sources

- [Official README](https://github.com/credfeto/credfeto-enum-source-generation#readme)
- [Project repository](https://github.com/credfeto/credfeto-enum-source-generation)
- [Release notes](https://github.com/credfeto/credfeto-enum-source-generation/releases)

---

Hysen Labs editorial analysis, written from the project's own repository and release notes. Cite the canonical page: https://hysenlabs.com/projects/credfeto-credfeto-enum-source-generation
