Getting started

edit

Install the package

Add the Meziantou.Framework.FastEnumGenerator package to the project that declares the attribute:

dotnet package add Meziantou.Framework.FastEnumGenerator

The 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:

PropertyDefaultEffect
ExtensionMethodNamespaceThe namespace of the enumNamespace of the generated class.
IsPublictrueMakes 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