diff options
Diffstat (limited to 'Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs')
| -rw-r--r-- | Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs b/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs new file mode 100644 index 0000000..33529b8 --- /dev/null +++ b/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs @@ -0,0 +1,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();
+ }
+ }
+}
\ No newline at end of file |