getting an enum to a string is easy, but switching back can be a pain
If I have an enum:
public enum WhatToShow { All, Courses, Seminars };
and I want to turn a string "Courses" back into that enum Type, there are a few ways I could do it. The most basic way would be to use a switch statement; that is a pain, especially for large enums, plus it has to be re-written for each enum. Here is a simple extension you can use to convert strings back into enums:
public static T ToEnum<T>(this string strOfEnum)
{
return (T)Enum.Parse(typeof(T), strOfEnum);
}
Now if I have a simple string, it is simple to turn it back to an enum:
string str = "Courses";
WhatToShow en = str.ToEnum<WhatToShow>();