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
    {
        /// 
        /// 获取枚举描述
        /// 
        /// 
        /// 
        public static string GetDescription(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;
            }
        }

        /// 
        /// 获取枚举名称
        /// 
        /// 
        /// 
        /// 
        public static string GetEnumName(object value)
        {
            return Enum.GetName(typeof(T), value);
        }
    }

/// 
/// SMS 短信模板类型
/// 
public enum SmsTemplateType
{
    [Description("催付款")]
    CuiFuKuan = 1,

    [Description("发货提醒")]
    FaHuoTiXing = 2,

    [Description("同城提醒")]
    TongChengTiXing = 3,

    [Description("签收提醒")]
    QianShouTiXing = 4
}

转载于:https://www.cnblogs.com/minily/p/7390715.html

你可能感兴趣的:(C#遍历枚举)