/*-------------------------------------------------------------------------
* 功能描述:EnumExtensions
* 作者:xulisong
* 创建时间: 2019/6/17 10:43:13
* 版本号:v1.0
* -------------------------------------------------------------------------*/
using System.ComponentModel;
using System.Reflection;
namespace JBIM.Common
{
public static class EnumExtensions
{
///
/// 获取指定枚举类型的描述值
///
/// 枚举类型
///
public static string GetDescription(this T t) where T : struct
{
string description = t.ToString();
FieldInfo fieldInfo = t.GetType().GetField(description);
object[] attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
{
DescriptionAttribute info = attributes[0] as DescriptionAttribute;
if (info != null)
{
description = info.Description;
}
}
return description;
}
}
}