/*-------------------------------------------------------------------------
 * 功能描述：EnumExtensions
 * 作者：xulisong
 * 创建时间： 2019/6/17 10:43:13
 * 版本号：v1.0
 *  -------------------------------------------------------------------------*/

using System.ComponentModel;
using System.Reflection;

namespace JBIM.Common
{
    public static class EnumExtensions
    {
        /// <summary>
        /// 获取指定枚举类型的描述值
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <returns></returns>
        public static string GetDescription<T>(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;
        }
    }
}
