Может быть намного проще, если вы используете методы TryParse
или Parse
и ToObject
.
public static class EnumHelper
{
public static T GetEnumValue<T>(string str) where T : struct, IConvertible
{
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
throw new Exception("T must be an Enumeration type.");
}
T val;
return Enum.TryParse<T>(str, true, out val) ? val : default(T);
}
public static T GetEnumValue<T>(int intValue) where T : struct, IConvertible
{
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
throw new Exception("T must be an Enumeration type.");
}
return (T)Enum.ToObject(enumType, intValue);
}
}
Как отмечено в комментариях @chrfin, вы можете сделать это методом расширения очень легко просто добавив this
до типа параметра, который может быть удобен.