Getting started
editInstall the package
Add the Meziantou.Framework.FastEnumGenerator package to the project that declares the attribute:
dotnet package add Meziantou.Framework.FastEnumGeneratorThe package contains the source generator, the analyzer and its code fixes. The generator also adds FastEnumAttribute to the project, in the Meziantou.Framework.Annotations namespace. When the project enables implicit usings (<ImplicitUsings>enable</ImplicitUsings>), the package adds global using Meziantou.Framework.Annotations;; otherwise, add the using directive yourself.
Choose the enums
Add one FastEnumAttribute per enum, at the assembly level. It can be in any file of the project:
[assembly: FastEnumAttribute(typeof(Sample.Color), IsPublic = true, ExtensionMethodNamespace = "Sample.Extensions")]
namespace Sample;
public enum Color
{
Blue,
Red,
Green,
}The attribute takes the type of the enum and two optional properties:
| Property | Default | Effect |
|---|---|---|
ExtensionMethodNamespace | The namespace of the enum | Namespace of the generated class. |
IsPublic | true | Makes the generated methods public when the enum is visible outside the assembly. false keeps them internal. |
The generated methods of a public enum are public by default, so other assemblies can call them. They are always internal when the enum is internal, whatever IsPublic says.
Call the generated methods
The generated methods are extension methods of a class in the namespace of the enum, or in ExtensionMethodNamespace. Call sites need that namespace in scope:
using Sample.Extensions; // only needed when ExtensionMethodNamespace is used
var name = Color.Blue.ToStringFast();The class has a generated name that starts with FastEnumExtensions_ and is hidden from IntelliSense: you never use it directly.
Find the calls to update
Once an enum is listed, the analyzer reports the System.Enum calls on it that have a generated equivalent, such as value.ToString() (MFEG0008), and a code fix rewrites them. To keep your code unchanged, enable interceptors instead.
Next steps
- Generated members: what each method does, and the members that need C# 14.
- Interceptors: use the generated code without changing the call sites.