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(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(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(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(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(this T enumValue) { OldValueAttribute attribute = enumValue.GetAttribute(); if (attribute != null) { return attribute.OldValue; } return null; } public static string GetOldValue2(this T enumValue) { OldValue2Attribute attribute = enumValue.GetAttribute(); if (attribute != null) { return attribute.OldValue2; } return null; } public static int? GetOrder(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(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(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(this T enumValue) { return Convert.ToInt32(enumValue).ToString(); } } }