Try to parse Enum
namespace System
{
public static class EnumExtensions
{
/// <summary>
/// Tries the parse.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="theEnum">The enum.</param>
/// <param name="valueToParse">The value to parse.</param>
/// <param name="returnValue">The return value.</param>
/// <returns></returns>
public static bool TryParse<T>(this Enum theEnum, string valueToParse, out T returnValue)
{
returnValue = default(T);
int intEnumValue;
if (Int32.TryParse(valueToParse, out intEnumValue))
{
if (Enum.IsDefined(typeof (T), intEnumValue))
{
returnValue = (T) (object) intEnumValue;
return true;
}
}
return false;
}
}
}
Related examples in the same category