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

クローズ ジェネリック NULL 許容型を表す Type オブジェクト。

戻り値

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>型パラメーター リストを含む型宣言です。また、型パラメーター リストは 1 つ以上の型パラメーターを宣言します。 閉じたジェネリック型は、型パラメーターに特定の型が指定された型宣言です。

たとえば、パラメーターが nullableType C# の型 Nullable<Int32> (Nullable(Of Int32)Visual Basic) の場合、戻り値は型 Int32 (つまり、閉じたジェネリック型の型引数) になります。

適用対象