Over on this StackOverflow question, I found this really nice helper method for parsing a string into a generic Enum value:
public static T ParseEnum<T>(string value)
{
return (T) Enum.Parse(typeof(T), value, true);
}
I'd like to change this from a helper method into an extension method, but I'm not all that familiar with using generics, so I'm not sure if/how that would work.
I tried this, but as you'll quickly see this code doesn't actually compile:
public static T ParseEnum(this T myEnum, string value)
{
return (T) Enum.Parse(typeof(myEnum), value, true);
}
And I also tried this, but this doesn't do it either:
public static Enum ParseEnum(this Enum myEnum, string value)
{
return Enum.Parse(typeof(myEnum), value, true);
}
Is it even possible to do what I'm asking about here?