Ler em inglês

Compartilhar via


Nullable.GetUnderlyingType(Type) Método

Definição

Retorna o argumento de tipo subjacente do tipo que permite valor nulo especificado.

C#
public static Type GetUnderlyingType (Type nullableType);
C#
public static Type? GetUnderlyingType (Type nullableType);

Parâmetros

nullableType
Type

Um objeto Type que descreve um tipo que permite valor nulo genérico fechado.

Retornos

Type

O argumento de tipo do parâmetro nullableType, se o parâmetro nullableType for um tipo que permite valor nulo genérico fechado; caso contrário, null.

Exceções

nullableType é null.

Exemplos

O exemplo de código a seguir define um método cujo valor retornado é do tipo Nullable<T> .Int32 O exemplo de código usa o GetUnderlyingType método para exibir o argumento de tipo do valor retornado.

C#
// 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

*/

Comentários

Uma definição de tipo genérico é uma declaração de tipo, como Nullable<T>, que contém uma lista de parâmetros de tipo e a lista de parâmetros de tipo declara um ou mais parâmetros de tipo. Um tipo genérico fechado é uma declaração de tipo em que um tipo específico é especificado para um parâmetro de tipo.

Por exemplo, se o nullableType parâmetro for o tipo de Nullable<Int32> em C# (Nullable(Of Int32) em Visual Basic), o valor retornado será o tipo de Int32 (ou seja, o argumento de tipo do tipo genérico fechado).

Aplica-se a

Produto Versões
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0