Friday, 27 April 2018

c# - Create Generic method constraining T to an Enum



I'm building a function to extend the Enum.Parse concept that




  • Allows a default value to be parsed in case that an Enum value is not found

  • Is case insensitive




So I wrote the following:



public static T GetEnumFromString(string value, T defaultValue) where T : Enum
{
if (string.IsNullOrEmpty(value)) return defaultValue;
foreach (T item in Enum.GetValues(typeof(T)))
{
if (item.ToString().ToLower().Equals(value.Trim().ToLower())) return item;
}
return defaultValue;

}


I am getting a Error Constraint cannot be special class System.Enum.



Fair enough, but is there a workaround to allow a Generic Enum, or am I going to have to mimic the Parse function and pass a type as an attribute, which forces the ugly boxing requirement to your code.



EDIT All suggestions below have been greatly appreciated, thanks.



Have settled on (I've left the loop to maintain case insensitivity - I am using this when parsing XML)




public static class EnumUtils
{
public static T ParseEnum(string value, T defaultValue) where T : struct, IConvertible
{
if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type");
if (string.IsNullOrEmpty(value)) return defaultValue;

foreach (T item in Enum.GetValues(typeof(T)))
{

if (item.ToString().ToLower().Equals(value.Trim().ToLower())) return item;
}
return defaultValue;
}
}


EDIT: (16th Feb 2015) Julien Lebosquain has recently posted a compiler enforced type-safe generic solution in MSIL or F# below, which is well worth a look, and an upvote. I will remove this edit if the solution bubbles further up the page.


Answer



Since Enum Type implements IConvertible interface, a better implementation should be something like this:




public T GetEnumFromString(string value) where T : struct, IConvertible
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException("T must be an enumerated type");
}

//...
}



This will still permit passing of value types implementing IConvertible. The chances are rare though.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...