In vs2008, is it possible to write an extension methods which would apply to any enumeration.
I know you can write extension methods against a specific enumeration,
You can also implement conversion method as follows:
public static class Extensions
{
public static ConvertType Convert(this Enum e)
{
object o = null;
Type type = typeof(ConvertType);
if (type == typeof(int))
{
o = Convert.ToInt32(e);
}
else if (type == typeof(long))
{
o = Convert.ToInt64(e);
}
else if (type == typeof(short))
{
o = Convert.ToInt16(e);
}
else
{
o = Convert.ToString(e);
}
return (ConvertType)o;
}
}
Here is an example usage:
int a = MyEnum.A.Convert();