summaryrefslogtreecommitdiff
path: root/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs
blob: 8b5879c1c8211cc21e27442b8320c357bac2197a (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
using System;
using System.ComponentModel;
using System.Reflection;
using Gestor.Model.Attributes;

namespace Gestor.Common.Helpers;

public static class EnumHelper
{
	public static T2 GetAttribute<T, T2>(this T enumValue) where T2 : class
	{
		string name = Enum.GetName(enumValue.GetType(), enumValue);
		try
		{
			FieldInfo? field = enumValue.GetType().GetField(name);
			Type typeFromHandle = typeof(T2);
			return Attribute.GetCustomAttribute(field, typeFromHandle) as T2;
		}
		catch (Exception)
		{
			return null;
		}
	}

	public static T ToEnumByOldValue<T>(this string oldValue)
	{
		Type typeFromHandle = typeof(T);
		Type typeFromHandle2 = typeof(OldValueAttribute);
		FieldInfo[] fields = typeFromHandle.GetFields();
		foreach (FieldInfo fieldInfo in fields)
		{
			Attribute? customAttribute = Attribute.GetCustomAttribute(fieldInfo, typeFromHandle2);
			OldValueAttribute val = (OldValueAttribute)(object)((customAttribute is OldValueAttribute) ? customAttribute : null);
			if (val != null && val.OldValue == oldValue)
			{
				return (T)fieldInfo.GetValue(null);
			}
		}
		return default(T);
	}

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

	public static T ToEnumByOldValue2<T>(this string oldValue)
	{
		Type typeFromHandle = typeof(T);
		Type typeFromHandle2 = typeof(OldValue2Attribute);
		FieldInfo[] fields = typeFromHandle.GetFields();
		foreach (FieldInfo fieldInfo in fields)
		{
			Attribute? customAttribute = Attribute.GetCustomAttribute(fieldInfo, typeFromHandle2);
			OldValue2Attribute val = (OldValue2Attribute)(object)((customAttribute is OldValue2Attribute) ? customAttribute : null);
			if (val != null && val.OldValue2 == oldValue)
			{
				return (T)fieldInfo.GetValue(null);
			}
		}
		return default(T);
	}

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

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

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

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

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

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