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
<PackageReference Include="Beskar.CodeGeneration.EnumGenerator" Version="1.2.1" />
<PackageVersion Include="Beskar.CodeGeneration.EnumGenerator" Version="1.2.1" />
<PackageReference Include="Beskar.CodeGeneration.EnumGenerator" />
paket add Beskar.CodeGeneration.EnumGenerator --version 1.2.1
#r "nuget: Beskar.CodeGeneration.EnumGenerator, 1.2.1"
#:package Beskar.CodeGeneration.EnumGenerator@1.2.1
#addin nuget:?package=Beskar.CodeGeneration.EnumGenerator&version=1.2.1
#tool nuget:?package=Beskar.CodeGeneration.EnumGenerator&version=1.2.1
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
switchexpressions 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 | Versions 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. |
-
net10.0
- Beskar.CodeGeneration.EnumGenerator.Marker (>= 1.2.1)
- Me.Memory (>= 1.3.7)
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.