using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace EasyDevCore.Common
{
///
///
///
public static class EnumHelper
{
///
/// Determines whether the specified input has flags.
///
/// The input.
/// The args.
///
/// true if the specified input has flags; otherwise, false.
///
public static bool HasAny(this Enum input, params Enum[] args) => args.Any(v => input.HasFlag(v));
/// Gets the names.
/// The input.
///
///
///
public static List GetNames(this Enum input) => new List(Enum.GetNames(input.GetType()));
/// Gets the values.
///
///
///
///
public static IEnumerable GetValues() where T : Enum => Enum.GetValues(typeof(T)).Cast();
///
/// Gets the textual description of the enum if it has one. e.g.
///
///
/// enum UserColors
/// {
/// [Description("Bright Red")]
/// BrightRed
/// }
/// UserColors.BrightRed.ToDescription();
///
///
///
///
public static string GetDescription(this Enum input)
{
return input.GetType().GetField(input.ToString()).GetCustomAttribute()?.Description ?? input.ToString();
}
/// Gets the type code.
/// The enum.
///
///
///
public static TypeCode GetTypeCode(this Enum input) => Type.GetTypeCode(Enum.GetUnderlyingType(input.GetType()));
/// Determines whether [has] [the specified value].
///
/// The input.
/// The value.
///
/// true if [has] [the specified value]; otherwise, false.
/// Enums of type {input.GetType().Name}
public static bool Has(this Enum input, T value)
{
var typeCode = input.GetTypeCode();
switch (typeCode)
{
case TypeCode.Byte:
return (((byte)(object)input & (byte)(object)value) == (byte)(object)value);
case TypeCode.Int16:
return (((short)(object)input & (short)(object)value) == (short)(object)value);
case TypeCode.Int32:
return (((int)(object)input & (int)(object)value) == (int)(object)value);
case TypeCode.Int64:
return (((long)(object)input & (long)(object)value) == (long)(object)value);
default:
throw new NotSupportedException($"Enums of type {input.GetType().Name}");
}
}
/// Determines whether [is] [the specified value].
///
/// The input.
/// The value.
///
/// true if [is] [the specified value]; otherwise, false.
/// Enums of type {input.GetType().Name}
public static bool Is(this Enum input, T value)
{
var typeCode = input.GetTypeCode();
switch (typeCode)
{
case TypeCode.Byte:
return (byte)(object)input == (byte)(object)value;
case TypeCode.Int16:
return (short)(object)input == (short)(object)value;
case TypeCode.Int32:
return (int)(object)input == (int)(object)value;
case TypeCode.Int64:
return (long)(object)input == (long)(object)value;
default:
throw new NotSupportedException($"Enums of type {input.GetType().Name}");
}
}
/// Adds the specified value.
///
/// The input.
/// The value.
///
///
///
/// Enums of type {input.GetType().Name}
public static T Add(this Enum input, T value)
{
var typeCode = input.GetTypeCode();
switch (typeCode)
{
case TypeCode.Byte:
return (T)(object)((byte)(object)input | (byte)(object)value);
case TypeCode.Int16:
return (T)(object)((short)(object)input | (short)(object)value);
case TypeCode.Int32:
return (T)(object)((int)(object)input | (int)(object)value);
case TypeCode.Int64:
return (T)(object)((long)(object)input | (long)(object)value);
default:
throw new NotSupportedException($"Enums of type {input.GetType().Name}");
}
}
/// Removes the specified value.
///
/// The input.
/// The value.
///
///
///
/// Enums of type {input.GetType().Name}
public static T Remove(this Enum input, T value)
{
var typeCode = input.GetTypeCode();
switch (typeCode)
{
case TypeCode.Byte:
return (T)(object)((byte)(object)input & ~(byte)(object)value);
case TypeCode.Int16:
return (T)(object)((short)(object)input & ~(short)(object)value);
case TypeCode.Int32:
return (T)(object)((int)(object)input & ~(int)(object)value);
case TypeCode.Int64:
return (T)(object)((long)(object)input & ~(long)(object)value);
default:
throw new NotSupportedException($"Enums of type {input.GetType().Name}");
}
}
}
}