improve :my => 'code'
This came in handy for me, and I thought others might feel the same way...
Say you have one enumeration used as arguments for a method, and you created a simplified enumeration to override the original method to create a method that is easier to consume...
Example:
public enum XTypes{
PFM = 0,
STAT = 1,
YIELD = 2,
RANK = 3
}
and
public enum XTypesSimplified{
PFM = 0,
STAT = 1
}
You can use the following to convert one into another.
public class EnumHelper
{
public static T EnumToEnum<T, U>(U enumArg)
{
if (!typeof(T).IsEnum) throw new Exception("This method only takes enumerations.");
if (!typeof(U).IsEnum) throw new Exception("This method only takes enumerations.");
try
{
return (T) Enum.ToObject(typeof(T), enumArg);
}
catch
{
throw new Exception ( string.Format ("Error converting enumeration {0} value {1} to enumeration {2} " ,
enumArg.ToString(),
typeof(U).ToString(),
typeof(T).ToString()) );
}
}
}
Here's an example of using the code...
XTypesSimplified xtypesSimplified = XTypesSimplified.PFM;
XTypes xtypes = EnumHelper.EnumToEnum<XTypes, XTypesSimplified>(xtypesSimplified);
Comments welcome!
Jonathan