summaryrefslogtreecommitdiff
path: root/Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs
diff options
context:
space:
mode:
authorLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-03-30 17:17:46 +0000
committerLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-03-30 17:17:46 +0000
commit0440c722a221b8068bbf388c1c0c51f0faff0451 (patch)
tree169cbf90c50ff7961db82ecb606c50c2a45a1688 /Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs
parent225aa1499e37faf9d38257caabbadc68d78b427e (diff)
downloadgestor-master.tar.gz
gestor-master.zip
some dllsHEADmaster
Diffstat (limited to 'Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs')
-rw-r--r--Gestor.Common/Gestor.Common.Helpers/EnumHelper.cs115
1 files changed, 115 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..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<T, T2>(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<T>(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<T>(this T enumValue)
+ {
+ OldValueAttribute attribute = enumValue.GetAttribute<T, OldValueAttribute>();
+ if (attribute == null)
+ {
+ return null;
+ }
+ return attribute.OldValue;
+ }
+
+ public static T ToEnumByOldValue2<T>(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<T>(this T enumValue)
+ {
+ OldValue2Attribute attribute = enumValue.GetAttribute<T, OldValue2Attribute>();
+ if (attribute == null)
+ {
+ return null;
+ }
+ return attribute.OldValue2;
+ }
+
+ public static bool? GetDefault<T>(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<T>(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<T>(this T enumValue)
+ {
+ return Convert.ToInt32(enumValue).ToString();
+ }
+
+ public static string GetDescription<T>(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<T>(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;
+ }
+}