当前位置: 移动技术网 > IT编程>开发语言>.net > .net工具类 获取枚举类型的描述

.net工具类 获取枚举类型的描述

2018年11月03日  | 移动技术网IT编程  | 我要评论

万爱花年轻照片,郭佳美,惩罚的夜晚

一般情况我们会用枚举类型来存储一些状态信息,而这些信息有时候需要在前端展示,所以需要展示中文注释描述。

为了方便获取这些信息,就封装了一个枚举扩展类。

    /// <summary>
    /// 枚举扩展类
    /// </summary>
    public static class enumextension
    {
        /// <summary>
        /// 获取枚举的描述,需要descriptionattribute属性
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string getdescription(this enum e)
        {
            //获取枚举的type类型对象
            var type = e.gettype();
            //获取枚举的所有字段
            var fields = type.getfields();

            //遍历所有枚举的所有字段
            foreach (var field in fields)
            {
                if (field.name != e.tostring())
                {
                    continue;
                }
                //第二个参数true表示查找enumdisplaynameattribute的继承链

                if (field.isdefined(typeof(descriptionattribute), true))
                {
                    var attr = field.getcustomattribute(typeof(descriptionattribute), false) as descriptionattribute;
                    if (attr != null)
                    {
                        return attr.description;
                    }
                }
            }

            //如果没有找到自定义属性,直接返回属性项的名称
            return e.tostring();
        }

        /// <summary>
        /// 根据枚举获取下拉框列表
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static list<comboboxitemdto> getselectlist(this enum en)
        {
            var list = new list<comboboxitemdto>();

            foreach (var e in enum.getvalues(en.gettype()))
            {
                list.add(new comboboxitemdto() { displaytext = getdescription(e as enum), value = ((int)e).tostring(), isselected = e == en });
            }

            return list;
        }

        /// <summary>
        /// 根据枚举获取下拉框列表
        /// </summary>
        /// <param name="type">枚举类型</param>
        /// <returns></returns>
        public static list<comboboxitemdto> getselectlist(this type type)
        {
            var list = new list<comboboxitemdto>();

            foreach (var e in enum.getvalues(type))
            {
                list.add(new comboboxitemdto() { displaytext = getdescription(e as enum), value = ((int)e).tostring() });
            }

            return list;
        }
    }

上面代码中的 comboboxitemdto 类是来自 abp 源码,它主要用于提供前端下拉框的数据源。

    //
    // 摘要:
    //     this dto can be used as a simple item for a combobox/list.
    public class comboboxitemdto
    {
        //
        // 摘要:
        //     creates a new abp.application.services.dto.comboboxitemdto.
        public comboboxitemdto();
        //
        // 摘要:
        //     creates a new abp.application.services.dto.comboboxitemdto.
        //
        // 参数:
        //   value:
        //     value of the item
        //
        //   displaytext:
        //     display text of the item
        public comboboxitemdto(string value, string displaytext);

        //
        // 摘要:
        //     value of the item.
        public string value { get; set; }
        //
        // 摘要:
        //     display text of the item.
        public string displaytext { get; set; }
        //
        // 摘要:
        //     is selected?
        public bool isselected { get; set; }
    }

 

好了,下面来举个栗子,这是一个订单枚举类

    /// <summary>
    /// 商品订单状态
    /// </summary>
    public enum commodityorderstate
    {
        /// <summary>
        /// 待付款
        /// </summary>
        [description("待付款")]
        pendingpay,
        /// <summary>
        /// 待发货
        /// </summary>
        [description("待发货")]
        pendingship,
        /// <summary>
        /// 待收货
        /// </summary>
        [description("待收货")]
        pendingreceipt,
        /// <summary>
        /// 待评价
        /// </summary>
        [description("待评价")]
        pendingevaluation,
        /// <summary>
        /// 已评价
        /// </summary>
        [description("已评价")]
        evaluated,
        /// <summary>
        /// 已退款
        /// </summary>
        [description("已退款")]
        refunded = 100
    }

这是一个订单dto,一般会存在订单状态字段,就像这样。

        /// <summary>
        /// 订单状态(这个字段会通过automapper自动映射)
        /// </summary>
        public commodityorderstate state { get; set; }
        /// <summary>
        /// 订单状态描述
        /// </summary>
        public string statedesc => state.getdescription();

好了,这样前端就能拿到订单状态描述信息了,是不是很方便。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网