共用方式為


Nullable.GetUnderlyingType Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the underlying type argument of the specified nullable type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function GetUnderlyingType ( _
    nullableType As Type _
) As Type
public static Type GetUnderlyingType(
    Type nullableType
)

Parameters

  • nullableType
    Type: System.Type
    A Type object that describes a closed generic nullable type.

Return Value

Type: System.Type
The type argument of the nullableType parameter, if the nullableType parameter is a closed generic nullable type; otherwise, nulla null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
ArgumentNullException

nullableType is nulla null reference (Nothing in Visual Basic).

Remarks

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.

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).

Examples

The following code example defines a method whose return value is of type Nullable<T> of Int32. The code example uses the GetUnderlyingType method to display the type argument of the return value.

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

Imports System.Reflection

Class Example
   ' 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 Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim t As Type = GetType(Example)
      Dim mi As MethodInfo = t.GetMethod("MyMethod")
      Dim retval As Type = mi.ReturnType
      outputBlock.Text += String.Format("Return value type ... {0}", retval) & vbCrLf
      Dim answer As Type = Nullable.GetUnderlyingType(retval)
      outputBlock.Text += String.Format("Underlying type ..... {0}", answer) & vbCrLf

   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
'
// This code example demonstrates the
// Nullable.GetUnderlyingType() method.

using System;
using System.Reflection;

class Example
{
   /*
      Use reflection to obtain a Type object for the Sample 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 Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Type t = typeof(Sample);
      MethodInfo mi = t.GetMethod("MyMethod");
      Type retval = mi.ReturnType;
      outputBlock.Text += String.Format("Return value type ... {0}", retval) + "\n";
      Type answer = Nullable.GetUnderlyingType(retval);
      outputBlock.Text += String.Format("Underlying type ..... {0}", answer) + "\n";
   }
}

// Declare a type named Sample.
// The MyMethod member of Sample returns a Nullable of Int32.public class Sample
class Sample
{
    public int? MyMethod()
    {
        return 0;
    }
}
/*
This code example produces the following results:

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

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference