枚举的用法

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

原文地址:https://www.cnblogs.com/youmingkuang/p/7844018.html