Installation¶
Requirements¶
- .NET 8.0, .NET 9.0, or .NET 10.0 (or any target that supports
netstandard2.0for the runtime) - C# 9.0 or later (for
partialclass support)
NuGet Package¶
Add the package to your project:
What Gets Installed¶
The package bundles two assemblies:
LayeredCraft.OptimizedEnums.Generator.dll— the Roslyn source generator, loaded by the compilerLayeredCraft.OptimizedEnums.dll— the runtime base classes (OptimizedEnum<TEnum, TValue>,OptimizedEnum<TEnum>)
Both are delivered automatically by the single NuGet package. No separate runtime package reference is needed.
Optional: JSON Serialization¶
To add source-generated System.Text.Json converter support, install the SystemTextJson package. It declares the core package as a dependency, so only one dotnet add is needed:
See JSON Serialization for usage details.
Verifying the Installation¶
After adding the package, define a type that inherits from OptimizedEnum<TEnum, TValue> and declare it partial. Build the project — the generator runs during compilation and produces the lookup members. You can inspect the generated output under obj/ or via your IDE's "Go to definition" on any generated method.
using LayeredCraft.OptimizedEnums;
public sealed partial class OrderStatus : OptimizedEnum<OrderStatus, int>
{
public static readonly OrderStatus Pending = new(1, nameof(Pending));
private OrderStatus(int value, string name) : base(value, name) { }
}
// If installed correctly, this compiles:
var s = OrderStatus.FromName("Pending");
Tip
If FromName is not found after adding the package, see Troubleshooting.