How can I get the enum value if I have the enum string or enum int value. eg: If i have an enum as follows:
public enum TestEnum
{
Value1 = 1,
Value
There are numerous ways to do this, but if you want a simple example, this will do. It just needs to be enhanced with necessary defensive coding to check for type safety and invalid parsing, etc.
///
/// Extension method to return an enum value of type T for the given string.
///
///
///
///
public static T ToEnum(this string value)
{
return (T) Enum.Parse(typeof(T), value, true);
}
///
/// Extension method to return an enum value of type T for the given int.
///
///
///
///
public static T ToEnum(this int value)
{
var name = Enum.GetName(typeof(T), value);
return name.ToEnum();
}