From 0440c722a221b8068bbf388c1c0c51f0faff0451 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 14:17:46 -0300 Subject: some dlls --- Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs | 115 ++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs (limited to 'Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs') diff --git a/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs b/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs new file mode 100644 index 0000000..8b5879c --- /dev/null +++ b/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs @@ -0,0 +1,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(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; + } +} -- cgit v1.2.3