using System;
using System.ComponentModel;
using System.Reflection;
namespace WCS_Client.Extensions
{
public static class EnumExtensions
{
///
/// 扩展方法,获得枚举的Description
///
/// 枚举值
/// 当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用
/// 枚举的Description
public static string GetDescription(this Enum value, Boolean nameInstead = true)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
}
FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute == null && nameInstead == true)
{
return name;
}
return attribute?.Description;
}
}
}