需求:通过枚举类型的索引值获得枚举值
实现:
public static ScoreType GetScoreType(int index)//通过枚举类型的索引值获得枚举值
{
ScoreType scoreType = ScoreType.None;
foreach (ScoreType score in Enum.GetValues(typeof (ScoreType)))//Enum.GetValues(typeof (ScoreType))获得ScoreType枚举的所有内容(即一个数组array)
{
if (index == (int) score)
{
scoreType = score;
break;
}
}
return scoreType;
}
需求:枚举值转int
实现:这个没啥好说的,直接强转成int就完事了
需求:枚举值转string(也就是获得当前枚举值的名字)
实现:
public static string GetEnumName<T>(int index) where T : Enum
{
return Enum.GetName(typeof (T), index);
}