2019-06-07 08:59:07 +00:00
|
|
|
namespace System
|
|
|
|
{
|
|
|
|
public static class EnumExtension
|
|
|
|
{
|
2020-10-12 13:52:31 +00:00
|
|
|
public static T TryParseEnum<T>(string value, T defaultValue) where T : struct
|
2019-06-07 08:59:07 +00:00
|
|
|
{
|
2020-10-12 13:52:31 +00:00
|
|
|
return TryParseEnum(value, defaultValue, out _);
|
2019-06-07 08:59:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 13:52:31 +00:00
|
|
|
public static T TryParseEnum<T>(string value, T defaultValue, out bool isDefault) where T : struct
|
2019-06-07 08:59:07 +00:00
|
|
|
{
|
|
|
|
isDefault = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return (T)Enum.Parse(typeof(T), value, true);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
isDefault = true;
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|