1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 |
#region Using Statements
using System;
using System.ComponentModel;
#endregion
namespace Modules.Common
{
///<summary>
/// Handles operations for Enumerations
///</summary>
public static class EnumerationParser
{
/// <summary>
/// Reads the description attribute tag from an enum.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static string ReadDescription<T>(T value)
{
var type = typeof (T);
if (IsNullable(type))
type = UnderlyingTypeOf(type);
var fi = type.GetField(value.ToString());
var attributes = (DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}
/// <summary>
/// Parses the enum by description.
/// </summary>
/// <param name="descriptionValue">The description value.</param>
/// <returns></returns>
public static T ParseEnumByDescription<T>(string descriptionValue)
{
var type = typeof (T);
if (IsNullable(type))
type = UnderlyingTypeOf(type);
var names = Enum.GetNames(type);
foreach (string name in names)
{
var compare = Parse<T>(name);
if (ReadDescription(compare).Equals(descriptionValue, StringComparison.InvariantCultureIgnoreCase))
return compare;
}
throw new ArgumentException("The string is not a description or value of the specified enum.");
}
/// <summary>
/// Parses any string to the type of T: Enum
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <returns></returns>
public static T Parse<T>(string value)
{
return Parse<T>(value, false);
}
/// <summary>
/// Parses any string to the type of T: Enum
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
/// <returns></returns>
public static T Parse<T>(string value, bool ignoreCase)
{
if (string.IsNullOrEmpty(value) || string.Empty.Equals(value.Trim(), StringComparison.InvariantCultureIgnoreCase))
return default(T);
var type = typeof (T);
if (IsNullable(type))
type = UnderlyingTypeOf(type);
return (T) Enum.Parse(type, value.Trim(), ignoreCase);
}
/// <summary>
/// Determines whether the specified t is nullable.
/// </summary>
/// <param name="t">The t.</param>
/// <returns>
/// <c>true</c> if the specified t is nullable; otherwise, <c>false</c>.
/// </returns>
private static bool IsNullable(Type t)
{
if (!t.IsGenericType)
return false;
var g = t.GetGenericTypeDefinition();
return (g.Equals(typeof (Nullable<>)));
}
/// <summary>
/// Returns the underlying Type of T
/// </summary>
/// <param name="t">the type</param>
/// <returns></returns>
private static Type UnderlyingTypeOf(Type t)
{
return t.GetGenericArguments()[0];
}
}
} |