开发者

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();
    }       
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜