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
    
    }
  • 相关阅读:
    [APIO2014]序列分割
    [HNOI2008]玩具装箱TOY
    [ZJOI2007]时态同步
    [FJOI2014]最短路径树问题
    [IOI2011]Race
    [国家集训队]聪聪可可
    矩阵加速递推
    Codeforces Round #669 题意及思路
    Codeforces Round #670 题意及思路
    Codeforces Round #671 题意及思路
  • 原文地址:https://www.cnblogs.com/minily/p/7390715.html
Copyright © 2011-2022 走看看