Show / Hide Table of Contents

Enum Enhancements

EnumConverter allows to convert primitive types into arbitrary enum type and vice versa. It is helpful if enum type is defined as generic parameter and not known at compile time.

using DotNext;
using System;

enum Color: int
{
    Black = 0,
    Red = 0xFF0000,
    Green = 0x00FF00,
    Blue = 0x0000FF,
    White = 0xFFFFFF
}

public static E Plus<E>(E first, E second, E third) 
    where E: unmanaged, Enum
{
    var result = first.ToInt64() + second.ToInt64() + third.ToInt64();
    return result.ToEnum<E>();
}

var white = Plus(Color.Red, Color.Green, Color.Blue);
white == Color.White;   //true

Flag

System.Enum class has public instance method HasFlag that allows to check whether the one or more bits are set in the enumeration. However, it causes boxing of the argument. There is no generic version of this method in standard library to avoid boxing.

.NEXT library offers fast version of HasFlag method that prevents boxing. It's represented by static method located in Intrinsics class.

Attributes

EnumType static classes exposes access to custom attributes attached to the enum member.

using DotNext.Reflection;

sealed class EnumMemberAttribute : Attribute
{
}

enum MyEnum
{
    None = 0,

    [EnumMember]
    WithAttribute = 1,
}

EnumMemberAttribute attr = MyEnum.WithAttribute.GetCustomAttribute<MyEnum, EnumMemberAttribute>();
  • Improve this Doc
☀
☾
Back to top Generated by DocFX