Get underlying/derived type of enum?
How can you get the underlying/derived Type(byte, short, int, etc) of an e开发者_开发技巧num?
You are looking for Enum.GetUnderlyingType(enumType)
;
Sample from MSDN:
static object GetAsUnderlyingType(Enum enval)
{
Type entype = enval.GetType();
Type undertype = Enum.GetUnderlyingType(entype);
return Convert.ChangeType(enval, undertype);
}
using System;
class Program
{
enum IntEnum : int { A }
static void Main(string[] args)
{
var intEnum = IntEnum.A;
Console.WriteLine(intEnum.GetType().GetEnumUnderlyingType());
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
精彩评论