循环枚举获取枚举值的描述

//razor页面代码
@foreach (var ite in Enum.GetNames(typeof(MeasureType))) {

<th>@MeasureMinitorHelper.FetchEnumDescription((MeasureType)Enum.Parse(typeof(MeasureType), ite, false))</th> }

 

    /// <summary>
    /// 器具监测扩展帮助类
    /// </summary>
    public static class MeasureMinitorHelper
    {
        #region 

        /// <summary>
        /// 获取枚举值的描述文本
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string FetchEnumDescription(this Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes =
                  (DescriptionAttribute[])fi.GetCustomAttributes(
                  typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
        }

        #endregion
    }

 

posted @ 2019-07-11 15:04  蜗牛的礼物  阅读(258)  评论(0编辑  收藏  举报