Enum.GetUnderlyingType(Type) 方法

定義

傳回指定列舉的基礎類型。

public:
 static Type ^ GetUnderlyingType(Type ^ enumType);
public static Type GetUnderlyingType (Type enumType);
[System.Runtime.InteropServices.ComVisible(true)]
public static Type GetUnderlyingType (Type enumType);
static member GetUnderlyingType : Type -> Type
[<System.Runtime.InteropServices.ComVisible(true)>]
static member GetUnderlyingType : Type -> Type
Public Shared Function GetUnderlyingType (enumType As Type) As Type

參數

enumType
Type

要擷取其基礎類型的列舉。

傳回

Type

enumType 的基礎類型。

屬性

例外狀況

enumTypenull

enumType 不是 Enum

範例

下列範例會呼叫 GetUnderlyingType 方法,以顯示某些列舉成員的基礎型別。

using System;

public class Example
{
   public static void Main()
   {
      Enum[] enumValues = { ConsoleColor.Red, DayOfWeek.Monday,
                            MidpointRounding.ToEven, PlatformID.Win32NT,
                            DateTimeKind.Utc, StringComparison.Ordinal };
      Console.WriteLine("{0,-10} {1, 18}   {2,15}\n",
                        "Member", "Enumeration", "Underlying Type");
      foreach (var enumValue in enumValues)
         DisplayEnumInfo(enumValue);
   }

   static void DisplayEnumInfo(Enum enumValue)
   {
      Type enumType = enumValue.GetType();
      Type underlyingType = Enum.GetUnderlyingType(enumType);
      Console.WriteLine("{0,-10} {1, 18}   {2,15}",
                        enumValue, enumType.Name, underlyingType.Name);
   }
}
// The example displays the following output:
//       Member            Enumeration   Underlying Type
//
//       Red              ConsoleColor             Int32
//       Monday              DayOfWeek             Int32
//       ToEven       MidpointRounding             Int32
//       Win32NT            PlatformID             Int32
//       Utc              DateTimeKind             Int32
//       Ordinal      StringComparison             Int32
Module Example
   Public Sub Main()
      Dim enumValues() As [Enum] = { ConsoleColor.Red, DayOfWeek.Monday, 
                                     MidpointRounding.ToEven, PlatformID.Win32NT, 
                                     DateTimeKind.Utc, StringComparison.Ordinal }
      Console.WriteLine("{0,-10} {1, 18}   {2,15}", 
                        "Member", "Enumeration", "Underlying Type")
      Console.WriteLine()
      For Each enumValue In enumValues
         DisplayEnumInfo(enumValue)
      Next
   End Sub

   Sub DisplayEnumInfo(enumValue As [Enum])
      Dim enumType As Type = enumValue.GetType()
      Dim underlyingType As Type = [Enum].GetUnderlyingType(enumType)
      Console.WriteLine("{0,-10} {1, 18}   {2,15}",
                        enumValue, enumType.Name, underlyingType.Name)   
   End Sub
End Module
' The example displays the following output:
'       Member            Enumeration   Underlying Type
'       
'       Red              ConsoleColor             Int32
'       Monday              DayOfWeek             Int32
'       ToEven       MidpointRounding             Int32
'       Win32NT            PlatformID             Int32
'       Utc              DateTimeKind             Int32
'       Ordinal      StringComparison             Int32

備註

Enum 結構可讓值以命名常數表示。 列舉值的資料類型稱為其基礎類型。 例如,列舉的基礎類型 DayOfWeek ,其中包含代表一周每一天的常數 (DayOfWeek.MondayDayOfWeek.Tuesday 等等) ,則為 Int32

適用於

另請參閱