I think I'm probably doing something silly but I'm trying to write a generic function that takes a string
and converts it into an enum
(and then does some other stuff that I've skipped for sake of brevity).
The problem is, it complains that Enum.TryParse
needs a type that isn't nullable, it complains that T is nullable; Seemingly System.Enum
is nullable but actual enums aren't nullable.
Is there something I'm doing wrong here or is there a way around the problem.
private T GetEnumFilter<T>(string strValue) where T : Enum
{
return Enum.TryParse(strValue, out T value) ? value : throw new Exception("Invalid value");
}
I've seen this https://stackoverflow.com/a/8086788/1093406 answer and the sample at the dotnet samples and can't see what I've done wrong.