123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- namespace Wayne.Lib
- {
-
-
-
-
- public static class EnumSupport
- {
-
-
-
-
-
-
- public static string[] GetNames<TEnumType>()
- {
- Type type = typeof(TEnumType);
- if (!type.IsEnum)
- throw new ArgumentException("Type must be an enumeration.");
- List<string> names = new List<string>();
- int i = 0;
- while (System.Enum.IsDefined(type, i))
- {
- object oInt = i;
- TEnumType enumVal = (TEnumType)oInt;
- object oEnum = enumVal;
- names.Add((oEnum).ToString());
- i++;
- }
- return names.ToArray();
- }
-
-
-
-
-
-
- public static TEnumType[] GetValues<TEnumType>()
- {
- Type type = typeof(TEnumType);
- if (!type.IsEnum)
- throw new ArgumentException("Type must be an enumeration.");
- List<TEnumType> values = new List<TEnumType>();
- int i = 0;
- while (System.Enum.IsDefined(type, i))
- {
- object o = i;
- TEnumType et = (TEnumType)o;
- values.Add(et);
- i++;
- }
- return values.ToArray();
- }
-
-
-
-
-
-
-
-
- [DebuggerStepThrough]
- public static TEnumType Parse<TEnumType>(string value, bool ignoreCase, TEnumType defaultValue)
- {
- try
- {
- return (TEnumType)Enum.Parse(typeof(TEnumType), value, ignoreCase);
- }
- catch (ArgumentException)
- {
- return defaultValue;
- }
- }
-
-
-
-
-
-
-
-
-
- [DebuggerStepThrough]
- public static TEnumType? ParseDefaultNull<TEnumType>(string value, bool ignoreCase) where TEnumType : struct
- {
- if (!typeof(TEnumType).IsEnum)
- throw new ArgumentException("Type T Must be an Enum");
- if (string.IsNullOrEmpty(value))
- return null;
- try
- {
- return (TEnumType)Enum.Parse(typeof(TEnumType), value.Replace(typeof(TEnumType).Name + ".",""), ignoreCase);
- }
- catch (ArgumentException)
- {
- return null;
- }
- }
-
- }
- }
|