EnumHelper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace EasyDevCore.Common
  9. {
  10. /// <summary>
  11. /// <br />
  12. /// </summary>
  13. public static class EnumHelper
  14. {
  15. /// <summary>
  16. /// Determines whether the specified input has flags.
  17. /// </summary>
  18. /// <param name="input">The input.</param>
  19. /// <param name="args">The args.</param>
  20. /// <returns>
  21. /// <c>true</c> if the specified input has flags; otherwise, <c>false</c>.
  22. /// </returns>
  23. public static bool HasAny(this Enum input, params Enum[] args) => args.Any(v => input.HasFlag(v));
  24. /// <summary>Gets the names.</summary>
  25. /// <param name="input">The input.</param>
  26. /// <returns>
  27. /// <br />
  28. /// </returns>
  29. public static List<string> GetNames(this Enum input) => new List<string>(Enum.GetNames(input.GetType()));
  30. /// <summary>Gets the values.</summary>
  31. /// <typeparam name="T"></typeparam>
  32. /// <returns>
  33. /// <br />
  34. /// </returns>
  35. public static IEnumerable<T> GetValues<T>() where T : Enum => Enum.GetValues(typeof(T)).Cast<T>();
  36. /// <summary>
  37. /// Gets the textual description of the enum if it has one. e.g.
  38. ///
  39. /// <code>
  40. /// enum UserColors
  41. /// {
  42. /// [Description("Bright Red")]
  43. /// BrightRed
  44. /// }
  45. /// UserColors.BrightRed.ToDescription();
  46. /// </code>
  47. /// </summary>
  48. /// <param name="input"></param>
  49. /// <returns></returns>
  50. public static string GetDescription(this Enum input)
  51. {
  52. return input.GetType().GetField(input.ToString()).GetCustomAttribute<DescriptionAttribute>()?.Description ?? input.ToString();
  53. }
  54. /// <summary>Gets the type code.</summary>
  55. /// <param name="input">The enum.</param>
  56. /// <returns>
  57. /// <br />
  58. /// </returns>
  59. public static TypeCode GetTypeCode(this Enum input) => Type.GetTypeCode(Enum.GetUnderlyingType(input.GetType()));
  60. /// <summary>Determines whether [has] [the specified value].</summary>
  61. /// <typeparam name="T"></typeparam>
  62. /// <param name="input">The input.</param>
  63. /// <param name="value">The value.</param>
  64. /// <returns>
  65. /// <c>true</c> if [has] [the specified value]; otherwise, <c>false</c>.</returns>
  66. /// <exception cref="NotSupportedException">Enums of type {input.GetType().Name}</exception>
  67. public static bool Has<T>(this Enum input, T value)
  68. {
  69. var typeCode = input.GetTypeCode();
  70. switch (typeCode)
  71. {
  72. case TypeCode.Byte:
  73. return (((byte)(object)input & (byte)(object)value) == (byte)(object)value);
  74. case TypeCode.Int16:
  75. return (((short)(object)input & (short)(object)value) == (short)(object)value);
  76. case TypeCode.Int32:
  77. return (((int)(object)input & (int)(object)value) == (int)(object)value);
  78. case TypeCode.Int64:
  79. return (((long)(object)input & (long)(object)value) == (long)(object)value);
  80. default:
  81. throw new NotSupportedException($"Enums of type {input.GetType().Name}");
  82. }
  83. }
  84. /// <summary>Determines whether [is] [the specified value].</summary>
  85. /// <typeparam name="T"></typeparam>
  86. /// <param name="input">The input.</param>
  87. /// <param name="value">The value.</param>
  88. /// <returns>
  89. /// <c>true</c> if [is] [the specified value]; otherwise, <c>false</c>.</returns>
  90. /// <exception cref="NotSupportedException">Enums of type {input.GetType().Name}</exception>
  91. public static bool Is<T>(this Enum input, T value)
  92. {
  93. var typeCode = input.GetTypeCode();
  94. switch (typeCode)
  95. {
  96. case TypeCode.Byte:
  97. return (byte)(object)input == (byte)(object)value;
  98. case TypeCode.Int16:
  99. return (short)(object)input == (short)(object)value;
  100. case TypeCode.Int32:
  101. return (int)(object)input == (int)(object)value;
  102. case TypeCode.Int64:
  103. return (long)(object)input == (long)(object)value;
  104. default:
  105. throw new NotSupportedException($"Enums of type {input.GetType().Name}");
  106. }
  107. }
  108. /// <summary>Adds the specified value.</summary>
  109. /// <typeparam name="T"></typeparam>
  110. /// <param name="input">The input.</param>
  111. /// <param name="value">The value.</param>
  112. /// <returns>
  113. /// <br />
  114. /// </returns>
  115. /// <exception cref="NotSupportedException">Enums of type {input.GetType().Name}</exception>
  116. public static T Add<T>(this Enum input, T value)
  117. {
  118. var typeCode = input.GetTypeCode();
  119. switch (typeCode)
  120. {
  121. case TypeCode.Byte:
  122. return (T)(object)((byte)(object)input | (byte)(object)value);
  123. case TypeCode.Int16:
  124. return (T)(object)((short)(object)input | (short)(object)value);
  125. case TypeCode.Int32:
  126. return (T)(object)((int)(object)input | (int)(object)value);
  127. case TypeCode.Int64:
  128. return (T)(object)((long)(object)input | (long)(object)value);
  129. default:
  130. throw new NotSupportedException($"Enums of type {input.GetType().Name}");
  131. }
  132. }
  133. /// <summary>Removes the specified value.</summary>
  134. /// <typeparam name="T"></typeparam>
  135. /// <param name="input">The input.</param>
  136. /// <param name="value">The value.</param>
  137. /// <returns>
  138. /// <br />
  139. /// </returns>
  140. /// <exception cref="NotSupportedException">Enums of type {input.GetType().Name}</exception>
  141. public static T Remove<T>(this Enum input, T value)
  142. {
  143. var typeCode = input.GetTypeCode();
  144. switch (typeCode)
  145. {
  146. case TypeCode.Byte:
  147. return (T)(object)((byte)(object)input & ~(byte)(object)value);
  148. case TypeCode.Int16:
  149. return (T)(object)((short)(object)input & ~(short)(object)value);
  150. case TypeCode.Int32:
  151. return (T)(object)((int)(object)input & ~(int)(object)value);
  152. case TypeCode.Int64:
  153. return (T)(object)((long)(object)input & ~(long)(object)value);
  154. default:
  155. throw new NotSupportedException($"Enums of type {input.GetType().Name}");
  156. }
  157. }
  158. }
  159. }