Enum.GetUnderlyingType(Type) Méthode

Définition

Retourne le type sous-jacent de l’énumération spécifiée.

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

Paramètres

enumType
Type

Énumération dont le type sous-jacent sera récupéré.

Retours

Type sous-jacent de enumType.

Attributs

Exceptions

enumType a la valeur null.

enumType n’est pas un Enum.

Exemples

L’exemple suivant appelle la GetUnderlyingType méthode pour afficher le type sous-jacent de certains membres d’énumération.

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
open System

let displayEnumInfo (enumValue: Enum) =
    let enumType = enumValue.GetType()
    let underlyingType = Enum.GetUnderlyingType enumType
    printfn $"{enumValue,-10} {enumType.Name, 18}   {underlyingType.Name,15}"

let enumValues: Enum list =
    [ ConsoleColor.Red; DayOfWeek.Monday
      MidpointRounding.ToEven; PlatformID.Win32NT
      DateTimeKind.Utc; StringComparison.Ordinal ]

printfn "%-10s %18s   %15s\n" "Member" "Enumeration" "Underlying Type"
for enumValue in enumValues do
    displayEnumInfo enumValue

// 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

Remarques

La Enum structure permet de représenter les valeurs en tant que constantes nommées. Le type de données des valeurs de l’énumération est appelé son type sous-jacent. Par exemple, le type sous-jacent de l’énumération DayOfWeek , qui se compose de constantes qui représentent chaque jour de la semaine (DayOfWeek.Monday, DayOfWeek.Tuesdayet ainsi de suite), est Int32.

S’applique à

Voir aussi