码迷,mamicode.com
首页 > 其他好文 > 详细

Attribute 给枚举添加备注

时间:2015-11-05 12:08:48      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

记录

public class RemarkAttribute : Attribute
    {
        private string m_remark;
        public RemarkAttribute(string remark)
        {
            this.m_remark = remark;
        }
        public string Remark
        {
            get { return m_remark; }
            set { m_remark = value; }
        }
        public static string GetEnumRemark(Enum val)
        {
            Type type = val.GetType();
            FieldInfo fd = type.GetField(val.ToString());
            if (fd == null)
                return string.Empty;
            object[] attrs = fd.GetCustomAttributes(typeof(RemarkAttribute), false);
            string name = string.Empty;
            foreach (RemarkAttribute attr in attrs)
            {
                name = attr.Remark;
            }
            return name;
        }
    }
    /// <summary>
    /// 用户类型
    /// </summary>
    public enum userType
    {
        /// <summary>
        /// 会员
        /// </summary>
        [Remark("user")]
        user = 1,
        /// <summary>
        /// 商家
        /// </summary>
        [Remark("merchant")]
        merchant = 2,
        /// <summary>
        /// 月嫂
        /// </summary>
        [Remark("issuser")]
        issuser = 4
    }

 

Attribute 给枚举添加备注

标签:

原文地址:http://www.cnblogs.com/jmzs/p/4938966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!