Use an extension method to convert from string to enum.
public static T ToEnum(this string enumString)
{
return (T) Enum.Parse(typeof (T), enumString);
}
//Usage:
Color colorEnum = "Red".ToEnum<Color>();
Every Programming Best Practice, Interview Question, Algorithms
Use an extension method to convert from string to enum.
public static T ToEnum(this string enumString)
{
return (T) Enum.Parse(typeof (T), enumString);
}
//Usage:
Color colorEnum = "Red".ToEnum<Color>();
© Best Way to Code 2011 | Design by Blogger Hack Supported by PBT
0 comments:
Post a Comment