Say I have an enum,
public enum Colours
{
Red,
Blue
}
The only way I can see of parsing them is doing something like:
I believe that 4.0 has Enum.TryParse
Otherwise use an extension method:
public static bool TryParse(this Enum theEnum, string valueToParse, out T returnValue)
{
returnValue = default(T);
int intEnumValue;
if (Int32.TryParse(valueToParse, out intEnumValue))
{
if (Enum.IsDefined(typeof(T), intEnumValue))
{
returnValue = (T)(object)intEnumValue;
return true;
}
}
return false;
}