Nullable.GetUnderlyingType(Type) Nullable.GetUnderlyingType(Type) Nullable.GetUnderlyingType(Type) Nullable.GetUnderlyingType(Type) Method

Definition

Gibt das zugrunde liegende Typargument des angegebenen Typs zurück, für den NULL-Werte zulässig sind.Returns the underlying type argument of the specified nullable type.

public:
 static Type ^ GetUnderlyingType(Type ^ nullableType);
public static Type GetUnderlyingType (Type nullableType);
static member GetUnderlyingType : Type -> Type
Public Shared Function GetUnderlyingType (nullableType As Type) As Type

Parameter

nullableType
Type Type Type Type

Ein Type-Objekt, das einen geschlossenen generischen Typ beschreibt, für den NULL-Werte zulässig sind.A Type object that describes a closed generic nullable type.

Gibt zurück

Das Typargument des nullableType-Parameters, wenn der nullableType-Parameter ein geschlossener generischer Typ ist, für den NULL-Werte zulässig sind, andernfalls null.The type argument of the nullableType parameter, if the nullableType parameter is a closed generic nullable type; otherwise, null.

Ausnahmen

Beispiele

Das folgende Codebeispiel definiert eine Methode, deren Rückgabewert vom Typ ist Nullable<T> von Int32.The following code example defines a method whose return value is of type Nullable<T> of Int32. Das Codebeispiel verwendet die GetUnderlyingType Methode, um das Argument vom Typ des Rückgabewerts anzuzeigen.The code example uses the GetUnderlyingType method to display the type argument of the return value.

// This code example demonstrates the 
// Nullable.GetUnderlyingType() method.

using System;
using System.Reflection;

class Sample 
{
// Declare a type named Example. 
// The MyMethod member of Example returns a Nullable of Int32.

    public class Example
    {
        public int? MyMethod() 
        {
        return 0;
        }
    }
    
/* 
   Use reflection to obtain a Type object for the Example type.
   Use the Type object to obtain a MethodInfo object for the MyMethod method.
   Use the MethodInfo object to obtain the type of the return value of 
     MyMethod, which is Nullable of Int32.
   Use the GetUnderlyingType method to obtain the type argument of the 
     return value type, which is Int32.
*/   
    public static void Main() 
    {
        Type t = typeof(Example);
        MethodInfo mi = t.GetMethod("MyMethod");
        Type retval = mi.ReturnType;
        Console.WriteLine("Return value type ... {0}", retval);
        Type answer = Nullable.GetUnderlyingType(retval);
        Console.WriteLine("Underlying type ..... {0}", answer);
    }
}
/*
This code example produces the following results:

Return value type ... System.Nullable`1[System.Int32]
Underlying type ..... System.Int32

*/
' This code example demonstrates the 
' Nullable.GetUnderlyingType() method.

Imports System
Imports System.Reflection

Class Sample
    ' Declare a type named Example. 
    ' The MyMethod member of Example returns a Nullable of Int32.
    
    Public Class Example
        Public Function MyMethod() As Nullable(Of Integer)
            Return 0
        End Function
    End Class 'Example
    
' 
'   Use reflection to obtain a Type object for the Example type.
'   Use the Type object to obtain a MethodInfo object for the MyMethod method.
'   Use the MethodInfo object to obtain the type of the return value of 
'     MyMethod, which is Nullable of Int32.
'   Use the GetUnderlyingType method to obtain the type argument of the 
'     return value type, which is Int32.
'
    Public Shared Sub Main() 
        Dim t As Type = GetType(Example)
        Dim mi As MethodInfo = t.GetMethod("MyMethod")
        Dim retval As Type = mi.ReturnType
        Console.WriteLine("Return value type ... {0}", retval)
        Dim answer As Type = Nullable.GetUnderlyingType(retval)
        Console.WriteLine("Underlying type ..... {0}", answer)
    
    End Sub 'Main
End Class 'Sample
'
'This code example produces the following results:
'
'Return value type ... System.Nullable`1[System.Int32]
'Underlying type ..... System.Int32
'

Hinweise

Eine generische Typdefinition ist eine Typdeklaration, z. B. Nullable<T>, die eine Typparameterliste enthält und eine oder mehrere Typparameter deklariert.A generic type definition is a type declaration, such as Nullable<T>, that contains a type parameter list, and the type parameter list declares one or more type parameters. Ein geschlossener generischer Typ ist eine Typdeklaration, in denen ein bestimmter Typ für einen Typparameter angegeben ist.A closed generic type is a type declaration where a particular type is specified for a type parameter.

Z. B. wenn die nullableType Parameter ist der Typ des Nullable<Int32> in C# (Nullable(Of Int32) in Visual Basic), der Rückgabewert ist der Typ des Int32 (d. h. das Typargument des geschlossenen generischen Typs).For example, if the nullableType parameter is the type of Nullable<Int32> in C# (Nullable(Of Int32) in Visual Basic), the return value is the type of Int32 (that is, the type argument of the closed generic type).

Gilt für: