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

  • 相关阅读:
    NLog简单例子
    SQLite
    npm常用命令详解
    Nodejs全局安装和本地安装的区别
    C# 资源释放
    C#版本与Framework的关系
    .NET HTTP通用请求方法get/post
    log4net使用详解
    C# MongoDB--时区问题(差了8小时)
    MongoDB和Redis区别
  • 原文地址:https://www.cnblogs.com/youmingkuang/p/7844018.html
Copyright © 2011-2022 走看看