DynamicEnums 1.4.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package DynamicEnums --version 1.4.1                
NuGet\Install-Package DynamicEnums -Version 1.4.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="DynamicEnums" Version="1.4.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DynamicEnums --version 1.4.1                
#r "nuget: DynamicEnums, 1.4.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.
// Install DynamicEnums as a Cake Addin
#addin nuget:?package=DynamicEnums&version=1.4.1

// Install DynamicEnums as a Cake Tool
#tool nuget:?package=DynamicEnums&version=1.4.1                

DynamicEnums

Enum-like single-instance values with additional capabilities, including dynamic addition of new arbitrary values and flags

A dynamic enum uses BigInteger as its underlying type, allowing for an arbitrary number of enum values to be created, even when a Flags-like structure is used that would only allow for up to 64 values in a regular enum. All boolean operations including And<T>(T, T), Or<T>(T, T), Xor<T>(T, T) and Neg<T>(T) are supported and can be implemented in derived classes using operator overloads.

Setting it up

To create a custom dynamic enum, simply create a class that extends DynamicEnum. New values can then be added using Add<T>(string, BigInteger), AddValue<T>(string) or AddFlag<T>(string). In this example, they are added as static values in the class itself, but they can be added from anywhere.

public class MyEnum : DynamicEnum {
 
    // adding specifically defined values
    public static readonly MyEnum ValueOne = DynamicEnum.Add<MyEnum>("ValueOne", 1);
    public static readonly MyEnum ValueTwo = DynamicEnum.Add<MyEnum>("ValueTwo", 2);
    public static readonly MyEnum ValueThree = DynamicEnum.Add<MyEnum>("ValueThree", 3);
    
    // adding flags, which automatically uses the next available power of two as its value
    public static readonly MyEnum FlagOne = DynamicEnum.AddFlag<MyEnum>("FlagOne");
    public static readonly MyEnum FlagTwo = DynamicEnum.AddFlag<MyEnum>("FlagTwo");
    public static readonly MyEnum FlagThree = DynamicEnum.AddFlag<MyEnum>("FlagThree");
 
    // you can optionally create operator overloads for easier operations
    public static implicit operator BigInteger(MyEnum value) => DynamicEnum.GetValue(value);
    public static implicit operator MyEnum(BigInteger value) => DynamicEnum.GetEnumValue<MyEnum>(value);
    public static MyEnum operator |(MyEnum left, MyEnum right) => DynamicEnum.Or(left, right);
    public static MyEnum operator &(MyEnum left, MyEnum right) => DynamicEnum.And(left, right);
    public static MyEnum operator ^(MyEnum left, MyEnum right) => DynamicEnum.Xor(left, right);
    public static MyEnum operator ~(MyEnum value) => DynamicEnum.Neg(value);
}

Using it

Dynamic enums work very similarly to regular enums in how you use them, and each operation us optimized through an internal cache. Here are some examples of interactions with the dynamic enum we created above.

// getting the underlying value
BigInteger val1 = DynamicEnum.GetValue(MyEnum.FlagTwo); // using GetValue
BigInteger val2 = (BigInteger) MyEnum.FlagTwo; // using our operator overloads

// creating a combined flag
MyEnum allFlags1 = DynamicEnum.Or(MyEnum.FlagOne, DynamicEnum.Or(MyEnum.FlagTwo, MyEnum.FlagThree)); // using Or
MyEnum allFlags2 = MyEnum.FlagOne | MyEnum.FlagTwo | MyEnum.FlagThree; // using our operator overloads
MyEnum mixedFlags = DynamicEnum.Or(MyEnum.FlagOne, DynamicEnum.GetEnumValue<MyEnum>(17)); // using non-defined values in our combined flags

// querying flag information
bool hasAny = allFlags1.HasAnyFlags(MyEnum.FlagOne | MyEnum.ValueOne); // true
bool hasAll = allFlags1.HasAllFlags(MyEnum.FlagOne | MyEnum.ValueOne); // false

// displaying a dynamic enum value or flag
Console.WriteLine(MyEnum.FlagOne); // "FlagOne"
Console.WriteLine(allFlags1); // "FlagOne | FlagTwo | FlagThree"

// parsing a dynamic enum value
MyEnum parsed1 = DynamicEnum.Parse<MyEnum>("FlagOne");
MyEnum parsed2 = DynamicEnum.Parse<MyEnum>("FlagOne | FlagThree");

You can also check out the tests for some more complex examples.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5.2

  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 1,983 8/12/2024
1.4.1 134 8/9/2024
1.4.0 119 8/9/2024
1.3.0 730 6/28/2024
1.2.1 109 6/28/2024
1.2.0 2,029 2/4/2024
1.1.0 190 12/22/2023
1.0.1 2,310 11/1/2022
1.0.0 362 10/31/2022