diff --git a/Core/Extensions/EnumExtensions.cs b/Core/Extensions/EnumExtensions.cs index 889c0be6..1b87d5b0 100644 --- a/Core/Extensions/EnumExtensions.cs +++ b/Core/Extensions/EnumExtensions.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.ComponentModel; -using System.Globalization; using System.Linq; namespace Vanara.Extensions @@ -160,6 +159,72 @@ namespace Vanara.Extensions return ret; } + /// Converts an enumerated value to another fixed type. + /// The enumerated type. + /// The type of the result. + /// The value to convert. + /// The converted value. + /// The size of TResult cannot be smaller than the size of TEnum. + public static TResult To(this TEnum value) where TEnum : unmanaged, System.Enum where TResult : unmanaged, IConvertible + { + unsafe + { + if (sizeof(TResult) < sizeof(TEnum)) throw new ArgumentException("The size of TResult cannot be smaller than the size of TEnum."); + return *(TResult*)(void*)&value; + } + } + + /// Converts a fixed type to an enumerated value. + /// The type of the value to convert. + /// The enumerated type. + /// The value to convert. + /// The converted value. + /// The size of TEnum cannot be smaller than the size of TValue. + public static TEnum ToEnum(this TValue value) where TEnum : unmanaged, System.Enum where TValue : unmanaged, IConvertible + { + unsafe + { + if (sizeof(TEnum) < sizeof(TValue)) throw new ArgumentException("The size of TEnum cannot be smaller than the size of TValue."); + return *(TEnum*)(void*)&value; + } + } + + /// Converts a fixed type to an enumerated value. + /// The enumerated type. + /// The value to convert. + /// The converted value. + public static TEnum ToEnum(this byte value) where TEnum : unmanaged, System.Enum => ToEnum(value); + + /// Converts a fixed type to an enumerated value. + /// The enumerated type. + /// The value to convert. + /// The converted value. + public static TEnum ToEnum(this sbyte value) where TEnum : unmanaged, System.Enum => ToEnum(value); + + /// Converts a fixed type to an enumerated value. + /// The enumerated type. + /// The value to convert. + /// The converted value. + public static TEnum ToEnum(this ushort value) where TEnum : unmanaged, System.Enum => ToEnum(value); + + /// Converts a fixed type to an enumerated value. + /// The enumerated type. + /// The value to convert. + /// The converted value. + public static TEnum ToEnum(this short value) where TEnum : unmanaged, System.Enum => ToEnum(value); + + /// Converts a fixed type to an enumerated value. + /// The enumerated type. + /// The value to convert. + /// The converted value. + public static TEnum ToEnum(this uint value) where TEnum : unmanaged, System.Enum => ToEnum(value); + + /// Converts a fixed type to an enumerated value. + /// The enumerated type. + /// The value to convert. + /// The converted value. + public static TEnum ToEnum(this int value) where TEnum : unmanaged, System.Enum => ToEnum(value); + /// Checks if represents an enumeration and throws an exception if not. /// The to validate. ///