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

定義

指定した NULL 許容型の基になる型を返します。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

パラメーター

nullableType
Type Type Type Type

クローズ ジェネリック NULL 許容型を表す Type オブジェクト。A Type object that describes a closed generic nullable type.

戻り値

nullableType パラメーターがクローズ ジェネリック NULL 許容型の場合は nullableType パラメーターの型引数。それ以外の場合は nullThe type argument of the nullableType parameter, if the nullableType parameter is a closed generic nullable type; otherwise, null.

例外

nullableTypenullです。nullableType is null.

次のコード例は、戻り値は、型のメソッドを定義します。Nullable<T>Int32します。The following code example defines a method whose return value is of type Nullable<T> of Int32. コード例では、GetUnderlyingType戻り値の型引数を表示するメソッド。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
'

注釈

ジェネリック型定義の型宣言をなどはNullable<T>、型パラメーター リストを格納していると、型パラメーター リストが 1 つまたは複数の型パラメーターを宣言します。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. クローズ ジェネリック型は、型パラメーターに対して特定の種類が指定されている型宣言です。A closed generic type is a type declaration where a particular type is specified for a type parameter.

たとえば場合、nullableTypeパラメーターの型がNullable<Int32>(C#) (Nullable(Of Int32) Visual Basic)、戻り値の型がInt32(つまり、クローズ ジェネリック型の型引数)。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).

適用対象