即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

获取枚举的对应值

编程语言 pengdayong77 10℃ 0评论

首先定义一个扩展属性的类

/// 
    /// 自定义类的信息
    /// 
    [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
    public class ExtendsAttribute:Attribute
    {
        /// 
        /// 描述性信息
        /// 
        public String Description { get; set; }
        /// 
        /// 静态的值
        /// 
        public int Key { get; set; }
        public ExtendsAttribute(string description)
        {
            Description = description;
        }
        public ExtendsAttribute(int key,string description)
        {
            Key = key;
            Description = description;
        }
    }



然后在枚举类中加入属性类

 public  enum VirtualOrderStatus
    {
        /// 
        /// 待付款
        /// 
        [ExtendsAttribute(0, "待付款")]
        WaitPayment = 0,
        /// 
        /// 已付款
        /// 
        [ExtendsAttribute(1, "已付款")]
        Paid = 1,
        /// 
        /// 已发货
        /// 
        [ExtendsAttribute(2, "已发货")]
        SendGoods = 2,
        /// 
        /// 已收货
        /// 
        [ExtendsAttribute(3, "已收货")]
        Deliveries = 3,
        /// 
        /// 已完成
        /// 
        [ExtendsAttribute(4, "已完成")]
        Finished = 4,
        /// 
        ///已取消
        /// 
        [ExtendsAttribute(5, "已取消")]
        Canceled = 5
    }

获取枚举的中文值

        /// 
        /// 获取虚拟订单状态
        /// 
        /// 
        public static Dictionary GetVirtualOrderStatus()
        {
            Dictionary state = new Dictionary();
            Type orderStatus = typeof(Common.Enum.VirtualOrderStatus);
            string[] name = orderStatus.GetEnumNames();
            for (int i = 0; i < name.Length; i++)
            {
                System.Reflection.FieldInfo field = orderStatus.GetField(name[i]);
                Common.ExtendsAttribute extendAtrr = field.GetCustomAttributes(typeof(Common.ExtendsAttribute), false)[0] as Common.ExtendsAttribute;
                state.Add(extendAtrr.Key, extendAtrr.Description);
            }
            if (state.Count > 0)
            {
                return state;
            }
            return null;
        }








转载请注明:CodingBlog » 获取枚举的对应值

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情