Nullable.GetUnderlyingType(Type) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳指定可空型態的底層型別參數。
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參數是封閉的通用可空型態,則為參數的nullableType型別參數;否則,為 null。
例外狀況
nullableType 是 null。
範例
以下程式碼範例定義了一個回傳值型別 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 參數是 C# 中 Nullable<Int32> 型態(Visual Basic 中的 Nullable(Of Int32)),則回傳值為 Int32 型態(即閉合泛型的型別參數)。