I would like to have a method that will parse from a nullable database column an enum. I wrote this method below (and had to restrict T to a struct to make it compile).
It does compile, but I believe its wrong as Enums are not structs? If so, how do I restrict the generic method to say I am expecting a ValueType you don't have to complain at me that "Only non-nullable value type could be underlying of 'System.Nullable'
private static T? ParseEnum<T>(DataRow row, string columnName)
where T : struct
{
T? value = null;
try
{
if (row[columnName] != DBNull.Value)
{
value = (T)Enum.Parse(
typeof(T),
row[columnName].ToString(),
true);
}
}
catch (ArgumentException) { }
return value;
}