using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; namespace Kreta.Ellenorzo.WebApi.VN.Logic { /// /// Author: Kovács Kornél (DevKornél) Created On: 2019.06. /// internal static class EnumLogic { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] public static IEnumerable<(int Id, string Nev, string Leiras)> GetEnumWithDisplayNameAttribute(Type enumType) { foreach (object item in Enum.GetValues(enumType)) { (int Id, string Nev, string Leiras) returnObject = ((int)item, item.ToString(), null); FieldInfo fieldInfo = item.GetType().GetField(returnObject.Nev); if (fieldInfo != null) { DisplayAttribute attributes = (DisplayAttribute)fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault(); if (attributes != null) { returnObject.Leiras = attributes.GetName(); } } yield return returnObject; } } public static string GetDisplayNameAttribute(T enumValue) { string nev = enumValue.ToString(); if (string.IsNullOrWhiteSpace(nev)) { return null; } FieldInfo fieldInfo = typeof(T).GetField(nev); if (fieldInfo != null) { DisplayAttribute attributes = (DisplayAttribute)fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault(); if (attributes != null) { return attributes.GetName(); } } return null; } } }