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
}