EnumSupport.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /// Parse a string to an enumeration value given a defaul value if failed.
  59. /// </summary>
  60. /// <typeparam name="TEnumType">The enumeration type.</typeparam>
  61. /// <param name="value">A string containing the name or value to convert.</param>
  62. /// <param name="ignoreCase">If true, ignore case; otherwise, regard case.</param>
  63. /// <param name="defaultValue">The default value of the enumeration if the parsing fails.</param>
  64. /// <returns></returns>
  65. [DebuggerStepThrough]
  66. public static TEnumType Parse<TEnumType>(string value, bool ignoreCase, TEnumType defaultValue)
  67. {
  68. try
  69. {
  70. return (TEnumType)Enum.Parse(typeof(TEnumType), value, ignoreCase);
  71. }
  72. catch (ArgumentException)
  73. {
  74. return defaultValue;
  75. }
  76. }
  77. /// <summary>
  78. /// Parse a string to a nullable enumeration value. If it's impossible to parse the string to this
  79. /// enumeration type, null is returned.
  80. /// </summary>
  81. /// <typeparam name="TEnumType">The enumeration type.</typeparam>
  82. /// <param name="value">A string containing the name or value to convert.</param>
  83. /// <param name="ignoreCase">If true, ignore case; otherwise, regard case.</param>
  84. /// <returns></returns>
  85. /// <exception cref="ArgumentException">If TEnumType is not an enum.</exception>
  86. [DebuggerStepThrough]
  87. public static TEnumType? ParseDefaultNull<TEnumType>(string value, bool ignoreCase) where TEnumType : struct
  88. {
  89. if (!typeof(TEnumType).IsEnum)
  90. throw new ArgumentException("Type T Must be an Enum");
  91. if (string.IsNullOrEmpty(value))
  92. return null;
  93. try
  94. {
  95. return (TEnumType)Enum.Parse(typeof(TEnumType), value.Replace(typeof(TEnumType).Name + ".",""), ignoreCase);
  96. }
  97. catch (ArgumentException)
  98. {
  99. return null;
  100. }
  101. }
  102. }
  103. }