enum Days
{
Nothing=0,
Mon=1,
Stu=2
}
static void Main(string[] args)
{
foreach (int item in Enum.GetValues(typeof(Days)))
{
int value = item;
string name= Enum.GetName(typeof(Days), item);
}
}
/// <summary>
/// 枚举描述
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static String GetEnumDesc(Enum e)
{
FieldInfo fieldInfo = e.GetType().GetField(e.ToString());
if (fieldInfo != null)
{
DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (EnumAttributes.Length > 0)
{
return EnumAttributes[0].Description;
}
}
return e.ToString();
}
public static T ToEnum<T>(int value, T defaultT) where T : struct
{
string enumName = Enum.GetName(typeof(T), value);
return ToEnum<T>(enumName, defaultT);
}
public static T ToEnum<T>(string enumName, T defaultT) where T : struct
{
if (string.IsNullOrWhiteSpace(enumName))
{
return defaultT;
}
T result;
if (!Enum.TryParse<T>(enumName.Trim(), out result))
{
return defaultT;
}
if (Enum.IsDefined(typeof(T), result))
{
return result;
}
return defaultT;
}