Skip to main content
net11.0.NETStandard2.0

Generate optimized enum helper methods

Published
Jul 8, 2026
3mo ago
Total Downloads
3.3K
3,251

Install

dotnet add package Meziantou.Framework.FastEnumGenerator --version 3.0.1
ProductVersions
.NET
net11.0
.NET Standard
netstandard2.0

README

Meziantou.Framework.FastEnumGenerator

The source generator generates specialized enum helpers for selected enum types.

[assembly: FastEnumAttribute(typeof(Sample.Color), IsPublic = true, ExtensionMethodNamespace = "Sample.Extensions")]

namespace Sample
{
    public enum Color
    {
        Blue,
        Red,
        Green,
    }
}

Generated methods

For each configured enum, the generator emits these instance extension methods:

  • string ToStringFast(this TEnum value)
  • string ToStringFast(this TEnum value, bool useMetadata)
  • bool HasFlag(this TEnum instance, TEnum flag)
  • string GetName(this TEnum instance)

Method behavior:

  • ToStringFast returns the declared enum name without using reflection.
  • ToStringFast(..., useMetadata: true) uses metadata names when available.
  • GetName is the non-metadata version of ToStringFast.
  • HasFlag uses typed bitwise operations ((instance & flag) == flag).

When the target project supports C# 14 extension members, the generator also emits static members on extension(TEnum):

  • TEnum Parse(string value, bool ignoreCase)
  • TEnum Parse(ReadOnlySpan value, bool ignoreCase)
  • TEnum Parse(string value, bool ignoreCase, bool useMetadata)
  • TEnum Parse(ReadOnlySpan value, bool ignoreCase, bool useMetadata)
  • bool TryParse(string value, bool ignoreCase, out TEnum result)
  • bool TryParse(ReadOnlySpan value, bool ignoreCase, out TEnum result)
  • bool TryParse(string value, bool ignoreCase, bool useMetadata, out TEnum result)
  • bool TryParse(ReadOnlySpan value, bool ignoreCase, bool useMetadata, out TEnum result)
  • bool IsDefined(TEnum value)
  • ReadOnlySpan GetNames(bool useMetadata)
  • ReadOnlySpan GetValues()

Analyzer rules

The package also ships analyzers and code fixes for enums configured with FastEnumAttribute.

Id Category Description Severity Enabled
MFEG0001 FastEnumGenerator FastEnum target type is invalid Error ✔️
MFEG0002 FastEnumGenerator Use FastEnum Parse Warning ✔️
MFEG0003 FastEnumGenerator Use FastEnum TryParse Warning ✔️
MFEG0004 FastEnumGenerator Use FastEnum GetNames Warning ✔️
MFEG0005 FastEnumGenerator Use FastEnum GetValues Warning ✔️
MFEG0006 FastEnumGenerator Use FastEnum GetName Warning ✔️
MFEG0007 FastEnumGenerator Use FastEnum IsDefined Warning ✔️
MFEG0008 FastEnumGenerator Use FastEnum ToStringFast Warning ✔️

Metadata names

useMetadata uses names from:

  • DisplayAttribute.Name
  • DisplayNameAttribute.DisplayName
  • EnumMemberAttribute.Value

When metadata is not available for a member, the declared enum name is used.

Additional resources

Changes from Previous Version

No structural changes between v3.0.0 and v3.0.1.

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

Compatibility metadata lists 2 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.