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

基底類別不支援叫用的方法。 衍生類別必須提供實作。

範例

下列程式碼範例會使用一般實例建立來建立建構類型的實例,然後使用 GetTypeGetGenericTypeDefinition 方法來擷取建構的類型和泛型型別定義。 這個範例會使用泛型 Dictionary<TKey,TValue> 型別;建構型別代表 Dictionary<TKey,TValue> 具有字串索引鍵之 Test 物件的 。

using namespace System;
using namespace System::Reflection;
using namespace System::Collections::Generic;

public ref class Test
{
public:
   static void Main()
   {
      Console::Write( L"\r\n--- Get the generic type that " );
      Console::WriteLine( L"defines a constructed type." );
     
      // Create a Dictionary of Test objects, using strings for the
      // keys.
      Dictionary< String^,Test^ >^ d = gcnew Dictionary< String^,Test^ >;
      
      // Get a Type object representing the constructed type.
      //
      Type^ constructed = d->GetType();
      DisplayTypeInfo( constructed );
      Type^ myGeneric = constructed->GetGenericTypeDefinition();
      DisplayTypeInfo( myGeneric );
   }

private:
   static void DisplayTypeInfo( Type^ t )
   {
      Console::WriteLine( L"\r\n{0}", t );
      Console::WriteLine( L"\tIs this a generic type definition? {0}",
         t->IsGenericTypeDefinition );
      Console::WriteLine( L"\tIs it a generic type? {0}",
         t->IsGenericType );
      array<Type^>^typeArguments = t->GetGenericArguments();
      Console::WriteLine( L"\tList type arguments ({0}):",
         typeArguments->Length );
      System::Collections::IEnumerator^ myEnum =
         typeArguments->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Type^ tParam = safe_cast<Type^>(myEnum->Current);
         Console::WriteLine( L"\t\t{0}", tParam );
      }
   }
};

int main()
{
   Test::Main();
}

/* 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
 */
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(Of T) 在 Visual Basic 或 generic <typename T> ref class G C++ 中) ,您可以建構和具現化 Visual Basic) 中的類型 G<int> (G(Of Integer) 。 假設物件 Type 代表這個建構型別,方法會 GetGenericTypeDefinition 傳回泛型型別定義。

如果使用相同的型別引數,從相同的泛型型別定義建立兩個建構型別,則 GetGenericTypeDefinition 方法會針對這兩種型別傳回相同的 Type 物件。

如果您在已經代表泛型型別定義的 物件上 Type 呼叫 GetGenericTypeDefinition 方法,它會傳回目前的 Type

重要

泛型型別的陣列本身不是泛型。 在 C# 程式碼或 Visual Basic 程式碼 A<int>[] v;Dim v() As A(Of Integer) 中,變數 v 的類型不是泛型。 在呼叫 GetGenericTypeDefinition 之前,使用 IsGenericType 來判斷類型是否為泛型。

如需泛型反映中所使用之規範的恆成立條件清單,請參閱 IsGenericType 屬性備註。

適用於

另請參閱