Soenneker.Gen.EnumValues
4.0.19
Prefix Reserved
dotnet add package Soenneker.Gen.EnumValues --version 4.0.19
NuGet\Install-Package Soenneker.Gen.EnumValues -Version 4.0.19
<PackageReference Include="Soenneker.Gen.EnumValues" Version="4.0.19" />
<PackageVersion Include="Soenneker.Gen.EnumValues" Version="4.0.19" />
<PackageReference Include="Soenneker.Gen.EnumValues" />
paket add Soenneker.Gen.EnumValues --version 4.0.19
#r "nuget: Soenneker.Gen.EnumValues, 4.0.19"
#:package Soenneker.Gen.EnumValues@4.0.19
#addin nuget:?package=Soenneker.Gen.EnumValues&version=4.0.19
#tool nuget:?package=Soenneker.Gen.EnumValues&version=4.0.19
Soenneker.Gen.EnumValues
High performance source generated enum values.
Generate value objects with fast lookup APIs, switch-friendly constants, and built-in JSON serialization (System.Text.Json/Newtonsoft.Json).
Installation
dotnet add package Soenneker.Gen.EnumValues
Usage
Annotate a partial type with [EnumValue] (defaults to int) or [EnumValue<T>]:
using Soenneker.Gen.EnumValues;
[EnumValue]
public sealed partial class OrderStatus
{
public static readonly OrderStatus Pending = new(1);
public static readonly OrderStatus Completed = new(2);
}
using Soenneker.Gen.EnumValues;
[EnumValue<string>]
public sealed partial class ColorCode
{
public static readonly ColorCode Red = new("R");
public static readonly ColorCode Blue = new("B");
}
The generator emits:
List<MemberName>Valueconstants (for constant-friendly switch labels)TryFromValue(TValue value, out TEnum result)FromValue(TValue value)TryFromName(string name, out TEnum result)FromName(string name)
Lookups
if (OrderStatus.TryFromValue(1, out var pending))
{
// pending == OrderStatus.Pending
}
var completed = OrderStatus.FromValue(2);
if (ColorCode.TryFromName("Red", out var red))
{
// red == ColorCode.Red
}
Switching over values
Switch labels must be compile-time constants. The generator emits <MemberName>Value constants so you can switch efficiently on Value:
switch (orderStatus.Value)
{
case OrderStatus.PendingValue:
// ...
break;
case OrderStatus.CompletedValue:
// ...
break;
}
If your variable is already the raw value type (int, string, etc.), you can switch directly on that variable with the same constants.
Serialization
System.Text.Json is always supported and the converter is applied automatically.
Newtonsoft.Json is also supported automatically when your project references Newtonsoft.Json:
dotnet add package Newtonsoft.Json
After that, both serializers round-trip by Value.
Value and the value constructor are generated automatically if they do not already exist.
Composing types with [IncludeEnumValues]
You can reuse instances from another enum-value type by adding [IncludeEnumValues(typeof(SourceType))]. The generator merges your type’s own static instances with all instances from the source type. Order is deterministic: your own instances first (source order), then each included type’s instances in attribute order and source order.
Example: source type
[EnumValue<string>]
public sealed partial class CommonKeyword
{
public static readonly CommonKeyword Default = new("default");
public static readonly CommonKeyword Auto = new("auto");
public static readonly CommonKeyword None = new("none");
}
Example: composed type
[EnumValue<string>]
[IncludeEnumValues(typeof(CommonKeyword))]
public sealed partial class SortDirection
{
public static readonly SortDirection Ascending = new("asc");
public static readonly SortDirection Descending = new("desc");
}
SortDirection then has five instances: Ascending, Descending (own), then Default, Auto, None (from CommonKeyword). The generator emits static readonly fields for included instances (e.g. SortDirection.Default), so List, Values, TryFromValue, TryFromName, and JSON work for both own and included values.
Requirements
- The source type must be an enum-value type:
[EnumValue]or[EnumValue<T>]with the same value type as the target (e.g. both[EnumValue<string>]). - You can use multiple
[IncludeEnumValues(typeof(A))],[IncludeEnumValues(typeof(B))]; instances are merged in attribute order.
Collisions
- Value collision: If the same value appears in both the target and an included type, or in two included types, the generator reports an error (e.g. Duplicate enum value 'none' in SortDirection from CommonKeyword).
- Name collision: If the target (or an earlier included type) already has a member with the same name as an included instance, the generator reports an error (e.g. Member name 'None' in SortDirection conflicts with included member from CommonKeyword).
Notes
- The enum type must be
partial. - Top-level non-generic class/struct types are supported.
- Static instances must be initialized with a compile-time constant first constructor argument.
<MemberName>Valueconstants are emitted for const-compatible value types (for example: numeric types,string,char,bool).
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (49)
Showing the top 5 NuGet packages that depend on Soenneker.Gen.EnumValues:
| Package | Downloads |
|---|---|
|
Soenneker.Enums.JsonOptions
An enumeration for common JSON options |
|
|
Soenneker.Enums.JsonLibrary
An enumeration for common JSON library types |
|
|
Soenneker.Enums.DeployEnvironment
An enum for standard deployment environments |
|
|
Soenneker.Enums.UnitOfTime
An enumerator for describing an interval of time |
|
|
Soenneker.Blazor.TomSelect
A Blazor interop library for the select user control library, Tom Select |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.19 | 0 | 3/4/2026 |
| 4.0.18 | 0 | 3/4/2026 |
| 4.0.17 | 0 | 3/4/2026 |
| 4.0.15 | 5,256 | 3/3/2026 |
| 4.0.14 | 23,821 | 2/27/2026 |
| 4.0.13 | 1,803 | 2/27/2026 |
| 4.0.12 | 91 | 2/27/2026 |
| 4.0.11 | 16,433 | 2/27/2026 |
| 4.0.10 | 4,824 | 2/27/2026 |
| 4.0.9 | 21,687 | 2/26/2026 |
| 4.0.8 | 1,489 | 2/26/2026 |
| 4.0.7 | 11,287 | 2/26/2026 |
| 4.0.6 | 19,089 | 2/26/2026 |
| 4.0.4 | 2,394 | 2/25/2026 |
| 4.0.3 | 1,763 | 2/25/2026 |
| 4.0.2 | 131 | 2/25/2026 |
| 4.0.1 | 82 | 2/25/2026 |