Nullable.GetUnderlyingType(Type) 方法

定义

返回指定可以为 null 的类型的基础类型参数。

public:
 static Type ^ GetUnderlyingType(Type ^ nullableType);
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

参数

nullableType
Type

一个 Type 对象,描述关闭的泛型可以为 null 的类型。

返回

Type

如果 nullableType 参数为关闭的泛型可以为 null 的类型,则为 nullableType 参数的类型变量;否则为 null

例外

nullableTypenull

示例

下面的代码示例定义其返回值的类型Nullable<T>Int32的方法。 代码示例使用 GetUnderlyingType 该方法显示返回值的类型参数。

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

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

type Example() =
    member _.MyMethod() =
        Nullable 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.
*)
let t = typeof<Example>
let mi = t.GetMethod "MyMethod"
let retval = mi.ReturnType
printfn $"Return value type ... {retval}"
let answer = Nullable.GetUnderlyingType retval
printfn $"Underlying type ..... {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.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
    
' 
'   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
End Class
'
'This code example produces the following results:
'
'Return value type ... System.Nullable`1[System.Int32]
'Underlying type ..... System.Int32
'

注解

泛型类型定义是类型声明,例如 Nullable<T>,包含类型参数列表,类型参数列表声明一个或多个类型参数。 封闭泛型类型是类型声明,其中为类型参数指定了特定类型。

例如,如果nullableType参数是 Visual Basic) 中的 C# (Nullable(Of Int32)类型Nullable<Int32>,则返回值是 (的类型Int32,即关闭泛型类型的类型参数) 。

适用于