| name | LayeredCraft.OptimizedEnums |
| nuget | https://www.nuget.org/packages/LayeredCraft.OptimizedEnums/ |
| link | https://github.com/layeredcraft/optimized-enums |
| author | LayeredCraft |
A new way to define enums – and efficiently manage them
This is how you can use LayeredCraft.OptimizedEnums .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LayeredCraft.OptimizedEnums" Version="1.4.4" />
</ItemGroup>
</Project>
The code that you will use is
using EnumDemo;
CarTypes dacia1 = CarTypes.Dacia;
var dacia2 = CarTypes.FromName("Dacia");
Console.WriteLine($"CarTypes {CarTypes.Count}: {dacia1} {dacia2} ");
using LayeredCraft.OptimizedEnums;
namespace EnumDemo;
public sealed partial class CarTypes : OptimizedEnum<CarTypes, int>
{
public static readonly CarTypes None = new(0, nameof(None));
public static readonly CarTypes Dacia = new(1, nameof(Dacia));
public static readonly CarTypes Tesla = new(2, nameof(Tesla));
public static readonly CarTypes BMW = new(3, nameof(BMW));
public static readonly CarTypes Mercedes = new(4, nameof(Mercedes));
private CarTypes(int value, string name) : base(value, name) { }
}
The code that is generated is
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
namespace EnumDemo;
[global::System.CodeDom.Compiler.GeneratedCode("LayeredCraft.OptimizedEnums.Generator", "1.4.4.0")]
partial class CarTypes
{
private static readonly global::System.Collections.ObjectModel.ReadOnlyCollection<global::EnumDemo.CarTypes> s_all =
global::System.Array.AsReadOnly(new global::EnumDemo.CarTypes[]
{
None,
Dacia,
Tesla,
BMW,
Mercedes
});
private static readonly global::System.Collections.ObjectModel.ReadOnlyCollection<string> s_names =
global::System.Array.AsReadOnly(new string[]
{
None.Name,
Dacia.Name,
Tesla.Name,
BMW.Name,
Mercedes.Name
});
private static readonly global::System.Collections.ObjectModel.ReadOnlyCollection<int> s_values =
global::System.Array.AsReadOnly(new int[]
{
None.Value,
Dacia.Value,
Tesla.Value,
BMW.Value,
Mercedes.Value
});
private static readonly global::System.Collections.Generic.Dictionary<string, global::EnumDemo.CarTypes> s_byName =
new global::System.Collections.Generic.Dictionary<string, global::EnumDemo.CarTypes>(5, global::System.StringComparer.Ordinal)
{
[None.Name] = None,
[Dacia.Name] = Dacia,
[Tesla.Name] = Tesla,
[BMW.Name] = BMW,
[Mercedes.Name] = Mercedes
};
private static readonly global::System.Collections.Generic.Dictionary<int, global::EnumDemo.CarTypes> s_byValue =
new global::System.Collections.Generic.Dictionary<int, global::EnumDemo.CarTypes>(5)
{
[None.Value] = None,
[Dacia.Value] = Dacia,
[Tesla.Value] = Tesla,
[BMW.Value] = BMW,
[Mercedes.Value] = Mercedes
};
[global::System.CodeDom.Compiler.GeneratedCode("LayeredCraft.OptimizedEnums.Generator", "1.4.4.0")]
public static global::System.Collections.Generic.IReadOnlyList<global::EnumDemo.CarTypes> All => s_all;
[global::System.CodeDom.Compiler.GeneratedCode("LayeredCraft.OptimizedEnums.Generator", "1.4.4.0")]
public static global::System.Collections.Generic.IReadOnlyList<string> Names => s_names;
[global::System.CodeDom.Compiler.GeneratedCode("LayeredCraft.OptimizedEnums.Generator", "1.4.4.0")]
public static global::System.Collections.Generic.IReadOnlyList<int> Values => s_values;
[global::System.CodeDom.Compiler.GeneratedCode("LayeredCraft.OptimizedEnums.Generator", "1.4.4.0")]
public const int Count = 5;
[global::System.CodeDom.Compiler.GeneratedCode("LayeredCraft.OptimizedEnums.Generator", "1.4.4.0")]
public static global::EnumDemo.CarTypes FromName(string name)
{
if (!s_byName.TryGetValue(name, out var result))
throw new global::System.Collections.Generic.KeyNotFoundException(
$"'{name}' is not a valid name for CarTypes");
return result;
}
[global::System.CodeDom.Compiler.GeneratedCode("LayeredCraft.OptimizedEnums.Generator", "1.4.4.0")]
public static bool TryFromName(string name, [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out global::EnumDemo.CarTypes? result) =>
s_byName.TryGetValue(name, out result);
[global::System.CodeDom.Compiler.GeneratedCode("LayeredCraft.OptimizedEnums.Generator", "1.4.4.0")]
public static global::EnumDemo.CarTypes FromValue(int value)
{
if (!s_byValue.TryGetValue(value, out var result))
throw new global::System.Collections.Generic.KeyNotFoundException(
$"'{value}' is not a valid value for CarTypes");
return result;
}
[global::System.CodeDom.Compiler.GeneratedCode("LayeredCraft.OptimizedEnums.Generator", "1.4.4.0")]
public static bool TryFromValue(int value, [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out global::EnumDemo.CarTypes? result) =>
s_byValue.TryGetValue(value, out result);
[global::System.CodeDom.Compiler.GeneratedCode("LayeredCraft.OptimizedEnums.Generator", "1.4.4.0")]
public static bool ContainsName(string name) => s_byName.ContainsKey(name);
[global::System.CodeDom.Compiler.GeneratedCode("LayeredCraft.OptimizedEnums.Generator", "1.4.4.0")]
public static bool ContainsValue(int value) => s_byValue.ContainsKey(value);
}
// <auto-generated/>
#nullable enable
namespace EnumDemo;
public static partial class Mapper
{
public static partial global::EnumDemo.TypesCar ToTypesCar(global::EnumDemo.CarTypes value)
{
return value switch
{
global::EnumDemo.CarTypes.None => global::EnumDemo.TypesCar.CarNone,
global::EnumDemo.CarTypes.Dacia => global::EnumDemo.TypesCar.CarDacia,
global::EnumDemo.CarTypes.Tesla => global::EnumDemo.TypesCar.CarTesla,
global::EnumDemo.CarTypes.BMW => global::EnumDemo.TypesCar.CarBMW,
global::EnumDemo.CarTypes.Mercedes => global::EnumDemo.TypesCar.CarMercedes,
_ => throw new global::System.ArgumentOutOfRangeException(nameof(value), value, $"The value of enum \"EnumDemo.CarTypes\" is not supported."),
};
}
public static partial global::EnumDemo.CarTypes ToCarTypes(global::EnumDemo.TypesCar value)
{
return value switch
{
global::EnumDemo.TypesCar.CarNone => global::EnumDemo.CarTypes.None,
global::EnumDemo.TypesCar.CarDacia => global::EnumDemo.CarTypes.Dacia,
global::EnumDemo.TypesCar.CarTesla => global::EnumDemo.CarTypes.Tesla,
global::EnumDemo.TypesCar.CarBMW => global::EnumDemo.CarTypes.BMW,
global::EnumDemo.TypesCar.CarMercedes => global::EnumDemo.CarTypes.Mercedes,
_ => throw new global::System.ArgumentOutOfRangeException(nameof(value), value, $"The value of enum \"EnumDemo.TypesCar\" is not supported."),
};
}
}
// <auto-generated/>
#nullable enable
namespace Porticle.Enumly
{
[global::System.AttributeUsage(global::System.AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
internal sealed class EnumlyClassAttribute : global::System.Attribute { }
[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
internal sealed class EnumlyMapAttribute : global::System.Attribute
{
/// <summary>
/// Value of the source enum that should be mapped to <c>null</c> on the target side.
/// Only meaningful when the target return type is a nullable enum.
/// </summary>
public object? NullSourceValue { get; set; }
/// <summary>
/// Value of the target enum that is produced when the source value is <c>null</c>.
/// Only meaningful when the source parameter type is a nullable enum.
/// </summary>
public object? NullTargetValue { get; set; }
/// <summary>
/// When <c>true</c>, a <c>null</c> source value throws
/// <see cref="global::System.ArgumentNullException"/> at runtime instead of
/// producing a target. Only meaningful when the source parameter type is a
/// nullable enum. Mutually exclusive with <see cref="NullTargetValue"/>.
/// </summary>
public bool IgnoreNullSource { get; set; }
}
/// <summary>
/// Defines an explicit source->target enum value mapping for special cases.
/// Can be applied multiple times. Overrides by-name matching for the given source value.
/// </summary>
[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
internal sealed class EnumlyMapValueAttribute : global::System.Attribute
{
public EnumlyMapValueAttribute(object source, object target) { }
}
/// <summary>
/// Marks a value of the source enum as intentionally unmapped. Suppresses the
/// 'no matching target member' diagnostic (EM0001) for that value. At runtime
/// the ignored value will throw <see cref="global::System.ArgumentOutOfRangeException"/>
/// with a message identifying it as explicitly excluded.
/// Can be applied multiple times.
/// </summary>
[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
internal sealed class EnumlyIgnoreSourceAttribute : global::System.Attribute
{
public EnumlyIgnoreSourceAttribute(object value) { }
}
/// <summary>
/// Marks a value of the target enum as intentionally unreachable. Suppresses
/// the 'target value is not produced by any source mapping' diagnostic
/// (EM0008) for that value. Can be applied multiple times.
/// </summary>
[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
internal sealed class EnumlyIgnoreTargetAttribute : global::System.Attribute
{
public EnumlyIgnoreTargetAttribute(object value) { }
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/LayeredCraft.OptimizedEnums