Beskar.CodeGeneration.EnumGenerator 1.2.1

dotnet add package Beskar.CodeGeneration.EnumGenerator --version 1.2.1
                    
NuGet\Install-Package Beskar.CodeGeneration.EnumGenerator -Version 1.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Beskar.CodeGeneration.EnumGenerator" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Beskar.CodeGeneration.EnumGenerator" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Beskar.CodeGeneration.EnumGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Beskar.CodeGeneration.EnumGenerator --version 1.2.1
                    
#r "nuget: Beskar.CodeGeneration.EnumGenerator, 1.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Beskar.CodeGeneration.EnumGenerator@1.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Beskar.CodeGeneration.EnumGenerator&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Beskar.CodeGeneration.EnumGenerator&version=1.2.1
                    
Install as a Cake Tool

Beskar.CodeGeneration.EnumGenerator

Beskar.CodeGeneration.EnumGenerator is a high-performance C# source generator that creates efficient extension methods for your enums. It eliminates the overhead of reflection-based operations like ToString(), Enum.Parse(), and Enum.IsDefined().

✨ Key Features

  • Performance: Zero-reflection ToString() and parsing replacements.
  • Modern C#: Utilizes latest language features like switch expressions and collection expressions.
  • Utility Methods: Easy access to all values and names without Enum.GetValues.
  • Lightweight: Only generates what you need for the marked enums.

🚀 Getting Started

To enable generation for an enum, simply decorate it with the [FastEnum] attribute.

Example

using Beskar.CodeGeneration.EnumGenerator.Marker.Attributes;

namespace MyProject;

[FastEnum]
public enum UserRole
{
    Admin,
    Moderator,
    User,
    Guest
}

🛠 Generated Output

The generator will produce a partial class containing optimized extension methods using the extension syntax:

#nullable enable

// <auto-generated>
// This file is auto-generated by EnumGenerator[1.2.1].
// </auto-generated>

using System;

namespace MyProject;

public static partial class UserRoleExtensions
{
   extension(global::MyProject.UserRole value)
   {
      public static int ValueCount => 4;

      public static global::MyProject.UserRole[] GetAllValues() => [
         global::MyProject.UserRole.Admin,
         global::MyProject.UserRole.Moderator,
         global::MyProject.UserRole.User,
         global::MyProject.UserRole.Guest,
      ];

      public static string[] GetAllNames() => [
         nameof(global::MyProject.UserRole.Admin),
         nameof(global::MyProject.UserRole.Moderator),
         nameof(global::MyProject.UserRole.User),
         nameof(global::MyProject.UserRole.Guest),
      ];

      public string ToFastString()
      {
         return value switch
         {
            global::MyProject.UserRole.Admin => nameof(global::MyProject.UserRole.Admin),
            global::MyProject.UserRole.Moderator => nameof(global::MyProject.UserRole.Moderator),
            global::MyProject.UserRole.User => nameof(global::MyProject.UserRole.User),
            global::MyProject.UserRole.Guest => nameof(global::MyProject.UserRole.Guest),
            _ => value.ToString()
         };
      }

      public bool IsFastDefined()
      {
         return value switch
         {
            global::MyProject.UserRole.Admin => true,
            global::MyProject.UserRole.Moderator => true,
            global::MyProject.UserRole.User => true,
            global::MyProject.UserRole.Guest => true,
            _ => false
         };
      }

      public static bool TryFastParseExact(string? name, out global::MyProject.UserRole result)
      {
         switch (name)
         {
            case nameof(global::MyProject.UserRole.Admin):
               result = global::MyProject.UserRole.Admin;
               return true;
            case nameof(global::MyProject.UserRole.Moderator):
               result = global::MyProject.UserRole.Moderator;
               return true;
            case nameof(global::MyProject.UserRole.User):
               result = global::MyProject.UserRole.User;
               return true;
            case nameof(global::MyProject.UserRole.Guest):
               result = global::MyProject.UserRole.Guest;
               return true;
            default:
               result = default;
               return false;
         };
      }

      public static bool TryFastParseCase(string? name, out global::MyProject.UserRole result, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
      {
         switch (name)
         {
            case string str when str.Equals(nameof(global::MyProject.UserRole.Admin), comparisonType):
               result = global::MyProject.UserRole.Admin;
               return true;
            case string str when str.Equals(nameof(global::MyProject.UserRole.Moderator), comparisonType):
               result = global::MyProject.UserRole.Moderator;
               return true;
            case string str when str.Equals(nameof(global::MyProject.UserRole.User), comparisonType):
               result = global::MyProject.UserRole.User;
               return true;
            case string str when str.Equals(nameof(global::MyProject.UserRole.Guest), comparisonType):
               result = global::MyProject.UserRole.Guest;
               return true;
            default:
               result = default;
               return false;
         };
      }
   }
}
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Beskar.CodeGeneration.EnumGenerator:

Package Downloads
Beskar.CodeGeneration.ContentGenerator.Marker

Generate headless cms like structures for ef core.

Beskar.CodeGeneration.ContentGenerator

Generate headless cms like structures for ef core.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.1 87 4/29/2026
1.2.0 132 4/26/2026
1.1.9 100 4/25/2026
1.1.8 91 4/8/2026