Saturday, April 19, 2008

Generic Enum Parsing in C#


Generics, as it were, are passe to talk about these days. However, I found myself dealing with enumerations on Friday and was a little surprised there wasn't an approach to parsing these out in a more generic fashion. I scribbled the following, perhaps it will be of value to someone:

enum Test { 
public static T EnumParser<T>(string givenValue)
return (T)Enum.Parse(typeof(T), givenValue, true);
Test val = EnumParser<Test>("v1");


No comments: