identificationOfCultivatedL.../Infrastructure/Extensions/EnumTypeExtention.cs

66 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Infrastructure.Extensions
{
public static class EnumTypeExtention
{
public static T GetEnumByName<T>(this string name)
{
foreach (var memberInfo in typeof(T).GetMembers())
{
foreach (var attr in memberInfo.GetCustomAttributes(true))
{
var test = attr as DisplayAttribute;
if (test == null) continue;
if (test.Name == name)
{
var result = (T)Enum.Parse(typeof(T), memberInfo.Name);
return result;
}
}
}
return default(T);
}
public static string GetEnumName<T>(this T type, Enum enm) where T : Type
{
foreach (var memberInfo in type.GetMembers())
{
foreach (var attr in memberInfo.GetCustomAttributes(true))
{
var test = attr as DisplayAttribute;
if (test == null) continue;
if (memberInfo.Name == enm.ToString())
{
return test.Name;
}
}
}
return null;
}
public static string GetDescription(this Enum val)
{
var field = val.GetType().GetField(val.ToString());
var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
if (customAttribute == null) { return val.ToString(); }
else { return ((DescriptionAttribute)customAttribute).Description; }
}
}
}