EnumSupport.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. namespace Wayne.Lib
  5. {
  6. /// <summary>
  7. /// Enumsupport is a class that has the goal to provide the functionality offered by System.Enum class, but that
  8. /// are not available in compact framework. It is very limited in some aspects, so please read carefully the comments of each method.
  9. /// </summary>
  10. public static class EnumSupport
  11. {
  12. /// <summary>
  13. /// Gets a list of the enumerated value names for the specified enumeration type.
  14. /// NOTE: The enum must not have explicit values set, so there are gaps in the value range, it must start at 0 and go up to max.
  15. /// </summary>
  16. /// <typeparam name="TEnumType"></typeparam>
  17. /// <returns></returns>
  18. public static string[] GetNames<TEnumType>()
  19. {
  20. Type type = typeof(TEnumType);
  21. if (!type.IsEnum)
  22. throw new ArgumentException("Type must be an enumeration.");
  23. List<string> names = new List<string>();
  24. int i = 0;
  25. while (System.Enum.IsDefined(type, i))
  26. {
  27. object oInt = i;
  28. TEnumType enumVal = (TEnumType)oInt;
  29. object oEnum = enumVal;
  30. names.Add((oEnum).ToString());
  31. i++;
  32. }
  33. return names.ToArray();
  34. }
  35. /// <summary>
  36. /// Gets a list of the enumerated values for the specified enumeration type.
  37. /// NOTE: The enum must not have explicit values set, so there are gaps in the value range, it must start at 0 and go up to max.
  38. /// </summary>
  39. /// <typeparam name="TEnumType"></typeparam>
  40. /// <returns></returns>
  41. public static TEnumType[] GetValues<TEnumType>()
  42. {
  43. Type type = typeof(TEnumType);
  44. if (!type.IsEnum)
  45. throw new ArgumentException("Type must be an enumeration.");
  46. List<TEnumType> values = new List<TEnumType>();
  47. int i = 0;
  48. while (System.Enum.IsDefined(type, i))
  49. {
  50. object o = i;
  51. TEnumType et = (TEnumType)o;
  52. values.Add(et);
  53. i++;
  54. }
  55. return values.ToArray();
  56. }
  57. /// <summary>
  58. /// Gets a list of the enumerated values for the specified enumeration type.
  59. /// NOTE: The enum must not have explicit values set, so there are gaps in the value range, it must start at 0 and go up to max.
  60. /// </summary>
  61. /// <typeparam name="TEnumType"></typeparam>
  62. /// <param name="t">Only values of this type are returned</param>
  63. /// <returns></returns>
  64. public static List<object> GetValues<TEnumType>(Type t)
  65. {
  66. Type type = typeof(TEnumType);
  67. if (!type.IsEnum)
  68. throw new ArgumentException("Type must be an enumeration.");
  69. List<object> values = new List<object>();
  70. int i = 0;
  71. while (System.Enum.IsDefined(type, i))
  72. {
  73. object o = i;
  74. if (o != null && o.GetType().Equals(t))
  75. {
  76. values.Add(o);
  77. }
  78. i++;
  79. }
  80. return values;
  81. }
  82. /// <summary>
  83. /// Parse a string to an enumeration value given a defaul value if failed.
  84. /// </summary>
  85. /// <typeparam name="TEnumType">The enumeration type.</typeparam>
  86. /// <param name="value">A string containing the name or value to convert.</param>
  87. /// <param name="ignoreCase">If true, ignore case; otherwise, regard case.</param>
  88. /// <param name="defaultValue">The default value of the enumeration if the parsing fails.</param>
  89. /// <returns></returns>
  90. [DebuggerStepThrough]
  91. public static TEnumType Parse<TEnumType>(string value, bool ignoreCase, TEnumType defaultValue)
  92. {
  93. try
  94. {
  95. return (TEnumType)Enum.Parse(typeof(TEnumType), value, ignoreCase);
  96. }
  97. catch (ArgumentException)
  98. {
  99. return defaultValue;
  100. }
  101. }
  102. /// <summary>
  103. /// Parse a string to a nullable enumeration value. If it's impossible to parse the string to this
  104. /// enumeration type, null is returned.
  105. /// </summary>
  106. /// <typeparam name="TEnumType">The enumeration type.</typeparam>
  107. /// <param name="value">A string containing the name or value to convert.</param>
  108. /// <param name="ignoreCase">If true, ignore case; otherwise, regard case.</param>
  109. /// <returns></returns>
  110. /// <exception cref="ArgumentException">If TEnumType is not an enum.</exception>
  111. [DebuggerStepThrough]
  112. public static TEnumType? ParseDefaultNull<TEnumType>(string value, bool ignoreCase) where TEnumType : struct
  113. {
  114. if (!typeof(TEnumType).IsEnum)
  115. throw new ArgumentException("Type T Must be an Enum");
  116. if (string.IsNullOrEmpty(value))
  117. return null;
  118. try
  119. {
  120. return (TEnumType)Enum.Parse(typeof(TEnumType), value.Replace(typeof(TEnumType).Name + ".",""), ignoreCase);
  121. }
  122. catch (ArgumentException)
  123. {
  124. return null;
  125. }
  126. }
  127. }
  128. }