The generic version of Parse<TEnum>(String) was introduced in .NET Core 2.0.
So you can just write:
class Program
{
static void Main(string[] args)
{
var e = Enum.Parse<MyEnum>("Value1");
Console.WriteLine($"Enum values is: {e}");
}
}
enum MyEnum
{
Value1,
Value2
}
Just keep in mind this is not in "old" .Net Framework (.NET 4.8 and less) or in any .NET Standard. You need to target .NET Core >= 2 (or .NET >= 5 since Microsoft dropped "Core" naming).
There is generic version of TryParse<TEnum>(String, TEnum) since .NET Framework 4.0. So you can use it like that:
if (Enum.TryParse<MyEnum>("Value2", out var e2))
{
Console.WriteLine($"Enum values is: {e2}");
}
or create your own helper method like:
public static class EnumUtils
{
public static TEnum Parse<TEnum>(String value) where TEnum : struct
{
return (TEnum)Enum.Parse(typeof(TEnum), value);
}
}
...
var e3 = EnumUtils.Parse<MyEnum>("Value1");
and of course you can just use non-generic version until you migrate your project to newer .NET ;)
var e4 = (MyEnum)Enum.Parse(typeof(MyEnum), "Value1");