From 1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 10:38:18 -0300 Subject: chore: location --- .../Gestor.Common.Helpers/EnumHelper.cs | 166 +++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 Codemerx/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs (limited to 'Codemerx/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs') diff --git a/Codemerx/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs b/Codemerx/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs new file mode 100644 index 0000000..33529b8 --- /dev/null +++ b/Codemerx/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(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(); + } + } +} \ No newline at end of file -- cgit v1.2.3