语言

Type.GetGenericTypeDefinition 方法

定义

返回一个 Type 对象,该对象表示可从中构造当前泛型类型的泛型类型定义。

public:
 abstract Type ^ GetGenericTypeDefinition();
public:
 virtual Type ^ GetGenericTypeDefinition();
public abstract Type GetGenericTypeDefinition();
public virtual Type GetGenericTypeDefinition();
abstract member GetGenericTypeDefinition : unit -> Type
abstract member GetGenericTypeDefinition : unit -> Type
override this.GetGenericTypeDefinition : unit -> Type
Public MustOverride Function GetGenericTypeDefinition () As Type
Public Overridable Function GetGenericTypeDefinition () As Type

返回

一个 Type 对象,表示可从中构造当前类型的泛型类型。

例外

当前类型不是泛型类型。 也就是说, IsGenericType 返回 false。

基类不支持调用的方法。 派生类必须提供实现。

示例

下面的代码示例使用普通实例创建来创建构造类型的实例,然后使用 GetType 和 GetGenericTypeDefinition 方法检索构造的类型和泛型类型定义。 此示例使用泛型 Dictionary<TKey,TValue> 类型;构造的类型表示 Dictionary<TKey,TValue> 具有字符串键的对象 Test 。

using System;
using System.Reflection;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {
        Console.WriteLine("\r\n--- Get the generic type that defines a constructed type.");

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

        // Get a Type object representing the constructed type.
        //
        Type constructed = d.GetType();
        DisplayTypeInfo(constructed);

        Type generic = constructed.GetGenericTypeDefinition();
        DisplayTypeInfo(generic);
    }

    private static void DisplayTypeInfo(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);
        Type[] typeArguments = t.GetGenericArguments();
        Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);
        foreach (Type tParam in typeArguments)
        {
            Console.WriteLine("\t\t{0}", tParam);
        }
    }
}

/* This example produces the following output:

--- Get the generic type that defines a constructed type.

System.Collections.Generic.Dictionary`2[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`2[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey
                TValue
 */
open System
open System.Collections.Generic

type Test() = class end

let displayTypeInfo (t: Type) =
    printfn $"\n{t}"
    printfn $"\tIs this a generic type definition? {t.IsGenericTypeDefinition}"
    printfn $"\tIs it a generic type? {t.IsGenericType}"
    let typeArguments = t.GetGenericArguments()
    printfn $"\tList type arguments ({typeArguments.Length}):"
    for tParam in typeArguments do
        printfn $"\t\t{tParam}"

printfn "\n--- Get the generic type that defines a constructed type."

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

// Get a Type object representing the constructed type.
let constructed = d.GetType()
displayTypeInfo constructed

let generic = constructed.GetGenericTypeDefinition()
displayTypeInfo generic


(* This example produces the following output:

--- Get the generic type that defines a constructed type.

System.Collections.Generic.Dictionary`2[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`2[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey
                TValue
 *)
Imports System.Reflection
Imports System.Collections.Generic

Public Class Test
    Public Shared Sub Main() 
        Console.WriteLine(vbCrLf & "--- Get the generic type that defines a constructed type.")
        
        ' Create a Dictionary of Test objects, using strings for the
        ' keys.
        Dim d As New Dictionary(Of String, Test)
        
        ' Get a Type object representing the constructed type.
        '
        Dim constructed As Type = d.GetType()
        DisplayTypeInfo(constructed)
        
        Dim generic As Type = constructed.GetGenericTypeDefinition()
        DisplayTypeInfo(generic)
    End Sub
    
    Private Shared Sub DisplayTypeInfo(ByVal t As Type) 
        Console.WriteLine(vbCrLf & t.ToString())
        Console.WriteLine(vbTab & "Is this a generic type definition? " _
            & t.IsGenericTypeDefinition)
        Console.WriteLine(vbTab & "Is it a generic type? " _
            & t.IsGenericType)
        Dim typeArguments As Type() = t.GetGenericArguments()
        Console.WriteLine(vbTab & "List type arguments (" _
            & typeArguments.Length & "):")
        For Each tParam As Type In typeArguments
            Console.WriteLine(vbTab & vbTab & tParam.ToString())
        Next tParam
    End Sub
End Class

' This example produces the following output:
'
'--- Get the generic type that defines a constructed type.
'
'System.Collections.Generic.Dictionary`2[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`2[TKey,TValue]
'        Is this a generic type definition? True
'        Is it a generic type? True
'        List type arguments (2):
'                TKey
'                TValue
'

注解

泛型类型定义是一个模板,可从中构造其他类型。 例如,从 C# 泛型类型定义 G<T>中,可以构造和实例化该类型 G<int>。 Type给定表示此构造类型的对象,该方法GetGenericTypeDefinition将返回泛型类型定义。

如果使用同一类型参数从同一泛型类型定义创建两个构造类型,该方法 GetGenericTypeDefinition 将返回这两种类型的同一 Type 对象。

如果对GetGenericTypeDefinition已表示泛型类型定义的对象调用Type该方法,则返回当前Type类型定义。

Important

泛型类型的数组本身不是泛型。 在 C# 代码 A<int>[] v; 或 Visual Basic 代码 Dim v() As A(Of Integer)中,变量 v 的类型不是泛型。 用于 IsGenericType 确定类型在调用 GetGenericTypeDefinition前是否为泛型类型。

有关泛型反射中使用的术语的固定条件的列表,请参阅 IsGenericType 属性备注。

适用于

另请参阅