2021-04-08 20:09:59 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace ET
|
|
|
|
|
{
|
|
|
|
|
public static class EnumHelper
|
|
|
|
|
{
|
|
|
|
|
public static int EnumIndex<T>(int value)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
foreach (object v in Enum.GetValues(typeof (T)))
|
|
|
|
|
{
|
|
|
|
|
if ((int) v == value)
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2021-05-25 01:14:19 +08:00
|
|
|
|
public static T GetByIndex<T>(int index)
|
|
|
|
|
{
|
|
|
|
|
return (T)Enum.GetValues(typeof (T)).GetValue(index);
|
|
|
|
|
}
|
2021-04-08 20:09:59 +08:00
|
|
|
|
|
|
|
|
|
public static T FromString<T>(string str)
|
|
|
|
|
{
|
|
|
|
|
if (!Enum.IsDefined(typeof(T), str))
|
|
|
|
|
{
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
return (T)Enum.Parse(typeof(T), str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|