zoukankan      html  css  js  c++  java
  • 枚举的用法

    1、循环枚举用法

    foreach (var items in Enum.GetNames(typeof(Constant.cb_type)))  //Enum.GetValues() ,Constant.cb_type是一个枚举类型 ,items 是得到枚举名称
    {

      int item = Convert.ToInt32(Enum.Parse(typeof(Constant.cb_type), items)); // 这里是得到枚举的对应数值
      string name = Enum.GetName(typeof(Constant.cb_type), item); //得到对应的名称,用数值得到对应的名称
    }
      public enum cb_type
        { 
            白 = 1,   
            富 = 2,   
            美 = 4,  
        }

    2、得到描述

    public static class GetDescription
        {
            /// <summary>
            /// 获取描述信息
            /// </summary>
            /// <param name="en"></param>
            /// <returns></returns>
            public static string description(this Enum en)
            {
                Type type = en.GetType();
                MemberInfo[] memInfo = type.GetMember(en.ToString());
                if (memInfo != null && memInfo.Length > 0)
                {
                    object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                    if (attrs != null && attrs.Length > 0)
                        return ((DescriptionAttribute)attrs[0]).Description;
                }
                return en.ToString();
            }
        }
    
        public enum Sex
        {
            [Description("")]
            man = 1,
            [Description("")]
            woman = 2,
            [Description("其他")]
            other = 3
        }

    参考:https://www.cnblogs.com/kissdodog/archive/2013/01/16/2863515.html

  • 相关阅读:
    《编写高质量代码》读书笔记一
    [转] Markdown
    皓首穷经还是及时行乐!
    有用的iOS网站地址
    [股票] 入市
    https原理 就是两次http
    数据预处理
    重新建立程序员的应对方式
    ROC曲线手画
    机器学习的总结
  • 原文地址:https://www.cnblogs.com/youmingkuang/p/7844018.html
Copyright © 2011-2022 走看看