using System; using System.ComponentModel; using System.Reflection; using Gestor.Model.Attributes; namespace Gestor.Common.Helpers; public static class EnumHelper { public static T2 GetAttribute(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(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(this T enumValue) { OldValueAttribute attribute = enumValue.GetAttribute(); if (attribute == null) { return null; } return attribute.OldValue; } public static T ToEnumByOldValue2(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(this T enumValue) { OldValue2Attribute attribute = enumValue.GetAttribute(); if (attribute == null) { return null; } return attribute.OldValue2; } public static bool? GetDefault(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(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(this T enumValue) { return Convert.ToInt32(enumValue).ToString(); } public static string GetDescription(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(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; } }