Enum.GetUnderlyingType(Type) 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回指定枚举的基础类型。
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 -> TypePublic Shared Function GetUnderlyingType (enumType As Type) As Type参数
- enumType
- Type
基础类型将被检索的枚举。
返回
enumType 的基础类型。
- 属性
例外
enumType 为 null。
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.Monday 为 DayOfWeek.Tuesday Int32) 。