Last active
June 18, 2024 15:12
-
-
Save RiskyWilhelm/7c7d3dccc212476d065a86641b7ae419 to your computer and use it in GitHub Desktop.
Enum HasAny() Extension (C# >= 9) Highly optimized
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public static class EnumExtensions | |
{ | |
/// <summary> Checks if enum 'a' contains any of the value(s) from 'b' </summary> | |
public static bool HasAny<EnumType>(this EnumType a, EnumType b) | |
where EnumType : Enum | |
{ | |
// Check for the supported enum types | |
return a.GetTypeCode() switch | |
{ | |
// byte | |
TypeCode.Byte => HasAny(a, (byte)(object)b), | |
// sbyte | |
TypeCode.SByte => HasAny(a, (sbyte)(object)b), | |
// short | |
TypeCode.Int16 => HasAny(a, (short)(object)b), | |
// ushort | |
TypeCode.UInt16 => HasAny(a, (ushort)(object)b), | |
// int | |
TypeCode.Int32 => HasAny(a, (int)(object)b), | |
// uint | |
TypeCode.UInt32 => HasAny(a, (uint)(object)b), | |
// long | |
TypeCode.Int64 => HasAny(a, (long)(object)b), | |
// ulong | |
TypeCode.UInt64 => HasAny(a, (ulong)(object)b), | |
_ => throw new NotSupportedException("Unknown Error. This shouldn't be happened!"), | |
}; | |
} | |
/// <summary> Checks if enum 'a' contains any of the value(s) from 'b' </summary> | |
public static bool HasAny<EnumType>(this EnumType a, byte b) | |
where EnumType : Enum | |
{ | |
EnumUtils.ValidateFlagEnum<EnumType>(); | |
EnumUtils.ValidateUnderlyingType<EnumType, byte>(); | |
return ((byte)(object)a & b) != 0; | |
} | |
/// <summary> Checks if enum 'a' contains any of the value(s) from 'b' </summary> | |
public static bool HasAny<EnumType>(this EnumType a, sbyte b) | |
where EnumType : Enum | |
{ | |
EnumUtils.ValidateFlagEnum<EnumType>(); | |
EnumUtils.ValidateUnderlyingType<EnumType, sbyte>(); | |
return ((sbyte)(object)a & b) != 0; | |
} | |
/// <summary> Checks if enum 'a' contains any of the value(s) from 'b' </summary> | |
public static bool HasAny<EnumType>(this EnumType a, short b) | |
where EnumType : Enum | |
{ | |
EnumUtils.ValidateFlagEnum<EnumType>(); | |
EnumUtils.ValidateUnderlyingType<EnumType, short>(); | |
return ((short)(object)a & b) != 0; | |
} | |
/// <summary> Checks if enum 'a' contains any of the value(s) from 'b' </summary> | |
public static bool HasAny<EnumType>(this EnumType a, ushort b) | |
where EnumType : Enum | |
{ | |
EnumUtils.ValidateFlagEnum<EnumType>(); | |
EnumUtils.ValidateUnderlyingType<EnumType, ushort>(); | |
return ((ushort)(object)a & b) != 0; | |
} | |
/// <summary> Checks if enum 'a' contains any of the value(s) from 'b' </summary> | |
public static bool HasAny<EnumType>(this EnumType a, int b) | |
where EnumType : Enum | |
{ | |
EnumUtils.ValidateFlagEnum<EnumType>(); | |
EnumUtils.ValidateUnderlyingType<EnumType, int>(); | |
return ((int)(object)a & b) != 0; | |
} | |
/// <summary> Checks if enum 'a' contains any of the value(s) from 'b' </summary> | |
public static bool HasAny<EnumType>(this EnumType a, uint b) | |
where EnumType : Enum | |
{ | |
EnumUtils.ValidateFlagEnum<EnumType>(); | |
EnumUtils.ValidateUnderlyingType<EnumType, uint>(); | |
return ((uint)(object)a & b) != 0; | |
} | |
/// <summary> Checks if enum 'a' contains any of the value(s) from 'b' </summary> | |
public static bool HasAny<EnumType>(this EnumType a, long b) | |
where EnumType : Enum | |
{ | |
EnumUtils.ValidateFlagEnum<EnumType>(); | |
EnumUtils.ValidateUnderlyingType<EnumType, long>(); | |
return ((long)(object)a & b) != 0; | |
} | |
/// <summary> Checks if enum 'a' contains any of the value(s) from 'b' </summary> | |
public static bool HasAny<EnumType>(this EnumType a, ulong b) | |
where EnumType : Enum | |
{ | |
EnumUtils.ValidateFlagEnum<EnumType>(); | |
EnumUtils.ValidateUnderlyingType<EnumType, ulong>(); | |
return ((ulong)(object)a & b) != 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public static class EnumUtils | |
{ | |
/// <summary> Throws exception on mismatch </summary> | |
public static void ValidateUnderlyingType<EnumType, EnumUnderlyingType>() | |
where EnumType : Enum | |
where EnumUnderlyingType : struct | |
{ | |
if (Enum.GetUnderlyingType(typeof(EnumType)) != typeof(EnumUnderlyingType)) | |
throw new NotSupportedException("Underlying value Type of EnumType and EnumUnderlyingType must be same and enums only support; byte, sbyte, short, ushort, int, uint, long, or ulong"); | |
} | |
public static void ValidateFlagEnum<T>() | |
where T : Enum | |
{ | |
if (!typeof(T).IsDefined(typeof(FlagsAttribute), inherit: false)) | |
throw new NotSupportedException("Only flag enums with Flags attribute supported"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment