CjmFastEnumLib 1.0.0.1-beta

This is a prerelease version of CjmFastEnumLib.
dotnet add package CjmFastEnumLib --version 1.0.0.1-beta                
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.
<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 and Polyglot Notebooks. 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 Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  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 was computed.  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 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  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.

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 1,049 11/20/2021
1.0.0-beta 209 11/13/2021

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