Ler em inglês

Compartilhar via


Type.IsGenericTypeDefinition Propriedade

Definição

Obtém um valor que indica se o Type atual representa uma definição de tipo genérico, da qual outros tipos genéricos podem ser construídos.

C#
public virtual bool IsGenericTypeDefinition { get; }

Valor da propriedade

true se o objeto Type representar uma definição de tipo genérico, caso contrário, false.

Exemplos

O exemplo a seguir exibe informações sobre um tipo, incluindo se é ou não uma definição de tipo genérico. As informações são exibidas para um tipo construído, para sua definição de tipo genérico e para um tipo comum.

C#
using System;
using System.Reflection;
using System.Collections.Generic;

public class Test
{
    private static void DisplayGenericTypeInfo(Type t)
    {
        Console.WriteLine("\r\n{0}", t);

        Console.WriteLine("\tIs this a generic type definition? {0}", 
            t.IsGenericTypeDefinition);

        Console.WriteLine("\tIs it a generic type? {0}", 
            t.IsGenericType);

        if (t.IsGenericType)
        {
            // If this is a generic type, display the type arguments.
            //
            Type[] typeArguments = t.GetGenericArguments();

            Console.WriteLine("\tList type arguments ({0}):", 
                typeArguments.Length);

            foreach (Type tParam in typeArguments)
            {
                // If this is a type parameter, display its
                // position.
                //
                if (tParam.IsGenericParameter)
                {
                    Console.WriteLine("\t\t{0}\t(unassigned - parameter position {1})",
                        tParam,
                        tParam.GenericParameterPosition);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
    }

    public static void Main()
    {
        Console.WriteLine("\r\n--- Display information about a constructed type, its");
        Console.WriteLine("    generic type definition, and an ordinary type.");

        // Create a Dictionary of Test objects, using strings for the
        // keys.       
        Dictionary<string, Test> d = new Dictionary<string, Test>();

        // Display information for the constructed type and its generic
        // type definition.
        DisplayGenericTypeInfo(d.GetType());
        DisplayGenericTypeInfo(d.GetType().GetGenericTypeDefinition());

        // Display information for an ordinary type.
        DisplayGenericTypeInfo(typeof(string));
    }
}

/* This example produces the following output:

--- Display information about a constructed type, its
    generic type definition, and an ordinary type.

System.Collections.Generic.Dictionary[System.String,Test]
        Is this a generic type definition? False
        Is it a generic type? True
        List type arguments (2):
                System.String
                Test

System.Collections.Generic.Dictionary[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey    (unassigned - parameter position 0)
                TValue  (unassigned - parameter position 1)

System.String
        Is this a generic type definition? False
        Is it a generic type? False
 */

Comentários

Uma definição de tipo genérico é um modelo do qual outros tipos podem ser construídos. Por exemplo, na definição G<T> de tipo genérico (expressa na sintaxe C#; G(Of T) no Visual Basic ou generic <typename T> ref class G no C++), você pode construir e instanciar o tipo G<int> (G(Of Integer) no Visual Basic), chamando o MakeGenericType método com uma lista de argumentos genéricos que contém o Int32 tipo. Dado um Type objeto que representa esse tipo construído, o GetGenericTypeDefinition método obtém a definição de tipo genérico novamente.

Use a IsGenericTypeDefinition propriedade para determinar se você pode criar novos tipos do tipo atual. Se a IsGenericTypeDefinition propriedade retornar true, você poderá chamar o MakeGenericType método para criar novos tipos genéricos.

Para obter uma lista das condições invariáveis para termos usados na reflexão genérica, consulte os comentários da propriedade IsGenericType.

Aplica-se a

Produto Versões
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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, 4.8.1
.NET Standard 2.0, 2.1

Confira também