zoukankan      html  css  js  c++  java
  • C#遍历枚举

    public enum testenum { aa, bb, cc, dd };

    foreach (testenum item in Enum.GetValues(typeof(testenum)))
    {

    }
    Enum.GetValues(typeof(枚举的名称));可以获得指定枚举的枚举值。
    
    
    foreach (string item in Enum.GetNames(typeof(testenum)))
    {

    }
    Enum.GetNames(typeof(枚举的名称));可以获得指定枚举的枚举名称。
    
    
    public class StateEnum
    
        {
    
            /// <summary>
    
            /// 获取枚举描述
    
            /// </summary>
    
            /// <param name="value"></param>
    
            /// <returns></returns>
    
            public static string GetDescription<T>(object value)
    
            {
    
                try
    
                {
    
                    T e = (T)value;
    
                    DescriptionAttribute attribute = e.GetType()
    
                    .GetField(e.ToString())
    
                    .GetCustomAttributes(typeof(DescriptionAttribute), false)
    
                    .SingleOrDefault() as DescriptionAttribute;
    
                    return attribute == null ? value.ToString() : attribute.Description;
    
                }
    
                catch (Exception)
    
                {
    
                    
    
                    throw;
    
                }
    
            }
    
    
    
            /// <summary>
    
            /// 获取枚举名称
    
            /// </summary>
    
            /// <typeparam name="T"></typeparam>
    
            /// <param name="value"></param>
    
            /// <returns></returns>
    
            public static string GetEnumName<T>(object value)
    
            {
    
                return Enum.GetName(typeof(T), value);
    
            }
    
        }
    
    
    /// <summary>
    
    /// SMS 短信模板类型
    
    /// </summary>
    
    public enum SmsTemplateType
    
    {
    
        [Description("催付款")]
    
        CuiFuKuan = 1,
    
    
    
        [Description("发货提醒")]
    
        FaHuoTiXing = 2,
    
    
    
        [Description("同城提醒")]
    
        TongChengTiXing = 3,
    
    
    
        [Description("签收提醒")]
    
        QianShouTiXing = 4
    
    }
  • 相关阅读:
    jquerymobile动态添加元素之后不能正确渲染解决方法
    HTML5的新事件
    Visual Studio 2010扩展让JS与CSS实现折叠
    移动web开发--meta 之 viewport
    QR code二维码简介及Qrencode库的移植与使用
    http,socks5,socks4代理的区别
    关于代理服务器的原理及用法
    MTK的线刷工具Flash_Tool的常见错误码
    承认吧 --- SVN你根本就不会用,细说SVN的那点事儿
    SSH协议介绍 --- 有用
  • 原文地址:https://www.cnblogs.com/minily/p/7390715.html
Copyright © 2011-2022 走看看