summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs
blob: 33529b868069c3bee9613d5fb8c546de75ba7543 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using Gestor.Model.Attributes;
using System;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace Gestor.Common.Helpers
{
	public static class EnumHelper
	{
		public static T2 GetAttribute<T, T2>(this T enumValue)
		where T2 : class
		{
			T2 customAttribute;
			string name = Enum.GetName(enumValue.GetType(), enumValue);
			try
			{
				customAttribute = (T2)(Attribute.GetCustomAttribute(enumValue.GetType().GetField(name), typeof(T2)) as T2);
			}
			catch (Exception exception)
			{
				customAttribute = default(T2);
			}
			return customAttribute;
		}

		public static bool? GetDefault<T>(this T enumValue)
		{
			object customAttribute;
			string name = Enum.GetName(enumValue.GetType(), enumValue);
			FieldInfo field = enumValue.GetType().GetField(name);
			if (field == null)
			{
				customAttribute = null;
			}
			else
			{
				customAttribute = Attribute.GetCustomAttribute(field, typeof(DefaultAttribute)) as DefaultAttribute;
			}
			if (customAttribute != null)
			{
				return new bool?(((DefaultAttribute)customAttribute).DefaultProperty);
			}
			return null;
		}

		public static string GetDescription<T>(this T enumValue)
		{
			object customAttribute;
			string name = Enum.GetName(enumValue.GetType(), enumValue);
			if (name == null)
			{
				return null;
			}
			FieldInfo field = enumValue.GetType().GetField(name);
			if (field == null)
			{
				customAttribute = null;
			}
			else
			{
				customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
			}
			if (customAttribute != null)
			{
				return ((DescriptionAttribute)customAttribute).Description;
			}
			return null;
		}

		public static bool GetHidden<T>(this T enumValue)
		{
			object customAttribute;
			string name = Enum.GetName(enumValue.GetType(), enumValue);
			FieldInfo field = enumValue.GetType().GetField(name);
			if (field == null)
			{
				customAttribute = null;
			}
			else
			{
				customAttribute = Attribute.GetCustomAttribute(field, typeof(HiddenAttribute)) as HiddenAttribute;
			}
			return customAttribute != null;
		}

		public static string GetOldValue<T>(this T enumValue)
		{
			OldValueAttribute attribute = enumValue.GetAttribute<T, OldValueAttribute>();
			if (attribute != null)
			{
				return attribute.OldValue;
			}
			return null;
		}

		public static string GetOldValue2<T>(this T enumValue)
		{
			OldValue2Attribute attribute = enumValue.GetAttribute<T, OldValue2Attribute>();
			if (attribute != null)
			{
				return attribute.OldValue2;
			}
			return null;
		}

		public static int? GetOrder<T>(this T enumValue)
		{
			object customAttribute;
			string name = Enum.GetName(enumValue.GetType(), enumValue);
			FieldInfo field = enumValue.GetType().GetField(name);
			if (field == null)
			{
				customAttribute = null;
			}
			else
			{
				customAttribute = Attribute.GetCustomAttribute(field, typeof(OrderAttribute)) as OrderAttribute;
			}
			if (customAttribute != null)
			{
				return new int?(((OrderAttribute)customAttribute).OrderProperty);
			}
			return null;
		}

		public static T ToEnumByOldValue<T>(this string oldValue)
		{
			Type type = typeof(T);
			Type type1 = typeof(OldValueAttribute);
			FieldInfo[] fields = type.GetFields();
			for (int i = 0; i < (int)fields.Length; i++)
			{
				FieldInfo fieldInfo = fields[i];
				OldValueAttribute customAttribute = Attribute.GetCustomAttribute(fieldInfo, type1) as OldValueAttribute;
				if (customAttribute != null && customAttribute.OldValue == oldValue)
				{
					return (T)fieldInfo.GetValue(null);
				}
			}
			return default(T);
		}

		public static T ToEnumByOldValue2<T>(this string oldValue)
		{
			Type type = typeof(T);
			Type type1 = typeof(OldValue2Attribute);
			FieldInfo[] fields = type.GetFields();
			for (int i = 0; i < (int)fields.Length; i++)
			{
				FieldInfo fieldInfo = fields[i];
				OldValue2Attribute customAttribute = Attribute.GetCustomAttribute(fieldInfo, type1) as OldValue2Attribute;
				if (customAttribute != null && customAttribute.OldValue2 == oldValue)
				{
					return (T)fieldInfo.GetValue(null);
				}
			}
			return default(T);
		}

		public static string Value<T>(this T enumValue)
		{
			return Convert.ToInt32(enumValue).ToString();
		}
	}
}