zoukankan      html  css  js  c++  java
  • C#枚举案例

    一、普通调用

    	public enum NoticeType
            {
                Notice = 'A',
                LabRule = 'H',
                HotInformation = 'N',
                Column = 'C',
                All = '1',
                Null = '0'
            }
            private void button1_Click(object sender, EventArgs e)
            {
                //新建枚举类型
                NoticeType noticeType1 = NoticeType.Column;
    
                //把枚举类型转换为string d="Column"
                string d = noticeType1.ToString();
    
                //取得枚举类型的基数 'C'
                char dd = (char)noticeType1;
    
                //通过基数取得对应的枚举类型 
                NoticeType noticeType2 = (NoticeType)Char.Parse("A");//Notice
    
                //通过名称取得枚举类型 
                NoticeType noticeType3 = (NoticeType)Enum.Parse(typeof(NoticeType), "Notice");
            }
    

    二、获取描述信息

     		[Description("会员等级")]
            enum MemberLevel
            {
                [Description("金牌会员")]
                gold = 1,
                [Description("银牌会员")]
                silver = 2,
                [Description("铜牌会员")]
                copper = 3
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="value">枚举值</param>
            /// <param name="isTop">是否是顶级标题的描述信息</param>
            /// <returns></returns>
            public static string GetDescription(this Enum value, bool isTop = false)
            {
    
                Type enumType = value.GetType();
                DescriptionAttribute attr = null;
                if (isTop)
                {
                    attr = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute));
                }
                else
                {
                    // 获取枚举常数名称。
                    string name = Enum.GetName(enumType, value);
                    if (name != null)
                    {
                        // 获取枚举字段。
                        FieldInfo fieldInfo = enumType.GetField(name);
                        if (fieldInfo != null)
                        {
                            // 获取描述的属性。
                            attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
                        }
                    }
                }
    
                if (attr != null && !string.IsNullOrEmpty(attr.Description))
                    return attr.Description;
                else
                    return string.Empty;
            }
    

    调用

         		MemberLevel gold = MemberLevel.gold;
                Console.WriteLine(gold.GetDescription());
                System.Console.Read();
    

    本文来自博客园,作者:農碼一生,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/15763922.html


    技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
    个人开源代码链接:
    GitHub:https://github.com/ITMingliang
    Gitee:https://gitee.com/mingliang_it
    GitLab:https://gitlab.com/ITMingliang
    进开发学习交流群:

  • 相关阅读:
    eclipse快捷键
    Struts2框架(8)---Struts2的输入校验
    Struts2框架(5)---result结果集
    Spring框架(6)---AspectJ实现AOP
    Spring框架(4)---AOP讲解铺垫
    Spring框架(3)---IOC装配Bean(注解方式)
    Spring框架(2)---IOC装配Bean(xml配置方式)
    Spring框架(1)---Spring入门
    Mybatis框架(5)---动态sql
    Mybatis框架(4)---输入输出映射
  • 原文地址:https://www.cnblogs.com/wml-it/p/15763922.html
Copyright © 2011-2022 走看看