1: public class EnumUtilities<T> where T : struct
2: {
3: static EnumUtilities()
4: {
5: if (!typeof(T).IsEnum)
6: {
7: throw new ArgumentException("Parameter T must be of type Enum");
8: }
9: }
10:
11: /// <summary>
12: /// Returns the Enumeration as a DataTable
13: /// </summary>
14: /// <param name="valueHeader">The name of the columns that contains the enumeration value</param>
15: /// <param name="textHeader">The name of the columns that contains the enumeration text</param>
16: public static DataTable GetAsDataTable(string valueHeader, string textHeader)
17: {
18: DataTable dt = new DataTable();
19:
20: dt.Columns.Add(valueHeader, typeof(int));
21: dt.Columns.Add(textHeader, typeof(string));
22:
23: foreach (int val in Enum.GetValues(typeof(T)))
24: {
25: dt.Rows.Add(new object[] { val, GetName(val) });
26: }
27:
28: return dt;
29: }
30:
31: /// <summary>
32: /// Parses a string to it's enumeration equivalent
33: /// </summary>
34: /// <param name="memberName"></param>
36: public static T Parse(string memberName)
37: {
38: return (T)Enum.Parse(typeof(T), memberName);
39: }
40:
41: /// <summary>
42: /// Gets the text value of an enumeration value from it's integer value
43: /// </summary>
44: /// <param name="value"></param>
45: /// <returns></returns>
46: public static string GetName(int value)
47: {
48: return Enum.GetName(typeof(T), value);
49: }
50: }