CjmFastEnumLib 1.0.0.1-beta

.NET 5.0 .NET Standard 2.0 .NET Framework 4.8
This is a prerelease version of CjmFastEnumLib.
NuGet\Install-Package CjmFastEnumLib -Version 1.0.0.1-beta
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.
dotnet add package CjmFastEnumLib --version 1.0.0.1-beta
<PackageReference Include="CjmFastEnumLib" Version="1.0.0.1-beta" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CjmFastEnumLib --version 1.0.0.1-beta
#r "nuget: CjmFastEnumLib, 1.0.0.1-beta"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install CjmFastEnumLib as a Cake Addin
#addin nuget:?package=CjmFastEnumLib&version=1.0.0.1-beta&prerelease

// Install CjmFastEnumLib as a Cake Tool
#tool nuget:?package=CjmFastEnumLib&version=1.0.0.1-beta&prerelease

Summary

This very simple utility is used for fast conversions between enums and their underlying types and is useful in the generic context. Safety checks are made once per process per distinct pair of enum type and Underlying type. Once the safety check passes for a given enum and type Underlying type, no further safety checks will be performed for that combination. This utility can only be used to convert values back and forth between their enum form and the form of their exact underlying type.

Compatible Platforms

  • Net Framework 4.8
  • Net Standard 2.0
  • Net Standard 2.1
  • Net 5.0
  • Net 6.0

Usage Examples

Correct Usage Example

This example comes from the example code portion of this repository's unit tests.

public static void DemonstrateSuccessfulConversion()
{
    //OK -- UShortBacked is backed by ushort
    UShortBacked enumValue = UShortBacked.MaxValueMinusOne;
    ushort convertedToBacker = EnumConverterUtil.ConvertEnumToUnderlier<UShortBacked, ushort>(enumValue);
    UShortBacked andBack =
        EnumConverterUtil.ConvertUnderlierToEnumValue<UShortBacked, ushort>(convertedToBacker);
    Assert.True(enumValue == andBack && convertedToBacker == (ushort)enumValue);

    //Note that no checking is done as to whether a (correctly typed) backer is actually a defined value of the type.
    ushort bla = 0xbabe; //random ushort
    UShortBacked enumVal = EnumConverterUtil.ConvertUnderlierToEnumValue<UShortBacked, ushort>(bla);
    ushort roundTripped = EnumConverterUtil.ConvertEnumToUnderlier<UShortBacked, ushort>(enumVal);
    Assert.True(bla == roundTripped); //works just fine even though val not defined
}

The above amply demonstrates correct usage.

Incorrect Usage Example

This utility is only usable to convert enum values to their exact underlying type and back. Attempts to convert an enum to a type other than their exact backer or vice versa will result in a TypeInitializationException being thrown. The inner exception of the TypeInitializationException will be of type EnumAndUnderlierMismatchException<TEnum, TIncorrectBacker>. The following snippet demonstrates incorrect usage that will throw exceptions:

public static void DemonstrateExceptionOnMismatch()
{
    UShortBacked enumValue = UShortBacked.DefaultPlusOne;
    short incorrectBacker = 1;
    //This will throw a type initialization exception because UShortBacked is backed by ushort, not short
    try
    {
        EnumConverterUtil.ConvertUnderlierToEnumValue<UShortBacked, short>(incorrectBacker);
    }
    catch (TypeInitializationException ex) when
        (ex.InnerException is EnumAndUnderlierMismatchException<UShortBacked, short>)
    {
        //CORRECT BEHAVIOR
    }
    catch (DidntThrowException)
    {
        //INCORRECT -- should have thrown
    }
    catch
    {
        Assert.False(true, "It threw the wrong thing!");
    }

    //And in reverse
    try
    {
        EnumConverterUtil.ConvertEnumToUnderlier<UShortBacked, short>(enumValue);
    }
    catch (TypeInitializationException ex) when
        (ex.InnerException is EnumAndUnderlierMismatchException<UShortBacked, short>)
    {
        //CORRECT BEHAVIOR
    }
    catch (DidntThrowException)
    {
        //INCORRECT -- should have thrown
    }
    catch
    {
        Assert.False(true, "It threw the wrong thing!");
    }
}

The foregoing should be sufficient to demonstrate the usage of this (very) limited-purpose library.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

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
1.0.0.1-beta 966 11/20/2021
1.0.0-beta 151 11/13/2021

This release adds methods that reinterpret arrays of enum types as arrays of their respective underliers and vice-versa.