mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
30 lines
500 B
C#
30 lines
500 B
C#
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;
|
|
}
|
|
|
|
public static T FromString<T>(string str)
|
|
{
|
|
if (!Enum.IsDefined(typeof(T), str))
|
|
{
|
|
return default(T);
|
|
}
|
|
return (T)Enum.Parse(typeof(T), str);
|
|
}
|
|
}
|
|
} |