Let's say I have an enum like
enum A
{
AA,
BB,
CC,
DD
}
I want to parse string values into Enum types, like "abc" -> AA, "BB" -> BB, "CC" -> CC, "DD" -> DD, as you can see AA has some special rule for the translation. So in this case Enum.TryParse<A>("abc", true, out AEnum)
would not serve the purpose. I can write some special codes to handle this exception value, and rest of values fall to use generic enum parser.
I have several enums with such exception rules, but if I come up with this type of codes
public static T GetEnumValue<T>(this string stringValue) where T : struct, IComparable, IConvertible, IFormattable
{
if (typeof(T).IsEnum)
{
if (typeof(T) == typeof(A))
{
T myEnum;
if (Enum.TryParse<T>(stringValue, true, out myEnum))
{
return myEnum;
}
else
{
// handle some special cases.
if (string.Compare(stringValue, "abc", StringComparison.OrdinalIgnoreCase) == 0)
{
return A.AA;
}
}
}
return default(T);
}
else
{
throw new Exception();
}
}
The code would not even compile, because compiler complains return value of that special case.
I strictly don't want to have attribute on my enum for some reason.
Anyone has good idea?