📜  如何在C#中获取枚举的类型代码?

📅  最后修改于: 2021-05-29 22:38:23             🧑  作者: Mango

Enum.GetTypeCode方法用于获取此枚举成员的基础类型的类型代码。

句法:

public TypeCode GetTypeCode ();

返回:此方法返回此实例的基础类型的类型代码。

异常:如果枚举类型未知,则此方法将提供InvalidOperationException

例子:

// C# program to illustrate the
// Enum.GetTypeCode() Method
using System;
  
class GFG {
      
    enum Color {Blue, Black };
  
    // Main Method
    public static void Main(String[] args)
    {
        Color c1 = Color.Blue;
        Console.Write("TypeCode of Enum Constant " + c1 + " : ");
  
        // Using the GetTypeCode() Method
        Console.WriteLine(c1.GetTypeCode());
  
        Color c2 = Color.Black;
        Console.Write("TypeCode of Enum Constant " + c2 + " : ");
  
        // Using the GetTypeCode Method
        Console.WriteLine(c2.GetTypeCode());
    }
}
输出:
TypeCode of Enum Constant Blue : Int32
TypeCode of Enum Constant Black : Int32

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.enum.gettypecode?view=netframework-4.8sfdsfsdf