Introduction
editMeziantou.Framework.FastEnumGenerator is a C# source generator. You list the enums to optimize with an assembly attribute, and the generator writes, at compile time, methods that convert them to strings, parse them and enumerate their values with code specialized for each enum, instead of the reflection that System.Enum uses:
[assembly: FastEnumAttribute(typeof(Sample.Color), IsPublic = true, ExtensionMethodNamespace = "Sample.Extensions")]
namespace Sample;
public enum Color
{
Blue,
Red,
Green,
}using Sample.Extensions; // only needed when ExtensionMethodNamespace is used
var name = Color.Blue.ToStringFast();Use it for the enums that hot paths format or parse: log fields, serialized values, HTTP headers, metrics tags...
What you can do with it
- Format values with
ToStringFastandGetName, and test flags withHasFlagFast. See Generated members. - Parse values and list the members with
Parse,TryParse,IsDefinedFast,GetNamesandGetValues, called on the enum type like theSystem.Enummethods (C# 14). - Use display names from
[Display]or[EnumMember]to format and parse values. - Speed up existing code without changing it, by letting the generator intercept the
System.Enumcalls. See Interceptors. - Find the calls to replace: the analyzer reports the
System.Enumcalls that have a generated equivalent, with a code fix. See Diagnostics.
Principles
- Same results as
System.Enum. The generated methods return what theSystem.Enummethod they replace returns, for declared values, undeclared values and[Flags]combinations. Only the way they compute it changes. - You choose the enums. Nothing is generated for an enum that is not listed in a
FastEnumAttribute. - Everything happens at compile time. The generated code is plain C# in your assembly, and the attribute is removed from the compiled assembly: there is no runtime dependency on the package.
- Opt-in rewrites. Interceptors change what an unchanged line of code does, so they are disabled until you enable them.
Next steps
- Getting started: install the package and generate the methods of an enum.
- Generated members: every generated method and how it behaves.