If I have a parameter of type System.Enum, how can I get the int representation of the contained value? I know if I had the actual Enum-derived class instance, I could just cast it to an int, but that doesn't seem to work on a System.Enum.
More succinctly, what is the correct body of this GetEnumValue function?
public enum Order
{
First,
Second,
Third
}
//Returns int representation of the passed enum.
//(e.g. should return 1 if passed Order.First, 2 if Order.Second, etc.)
//Should work on any other enum as well.
public int GetEnumValue(Enum enumInstance)
{
???
}
(I'm ignoring the issue that an arbitrary enum might not have int as its underlying type to simplify the problem. I'll need to allow for that as well, but the main wall I'm running into is simply not being able to get the value from an Enum param.)
ANSWER- Thanks to Chetan's comment, a solution is "return Convert.ToInt32(enumInstance);"
NOTE- This is not a duplicate of Get int value from enum in C# as some have commented. The important difference is I am asking about if the variable is of type System.Enum not a derived Enum. The simple int cast method in that answer does not work here.