Type.GetGenericTypeDefinition Type.GetGenericTypeDefinition Type.GetGenericTypeDefinition Type.GetGenericTypeDefinition Method

定義

傳回代表泛型類型定義的 Type 物件,利用此泛型類型定義就可以建構出目前的泛型類型。Returns a Type object that represents a generic type definition from which the current generic type can be constructed.

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

傳回

代表泛型類型的 Type 物件,利用此泛型類型就可以建構出目前的類型。A Type object representing a generic type from which the current type can be constructed.

例外狀況

目前的類型不是泛型類型。The current type is not a generic type. 亦即,IsGenericType 會傳回 falseThat is, IsGenericType returns false.

基底類別不支援叫用的方法。The invoked method is not supported in the base class. 衍生類別必須提供實作。Derived classes must provide an implementation.

範例

下列程式碼範例會建構類型的執行個體建立使用一般的執行個體建立,並接著會使用GetTypeGetGenericTypeDefinition方法來擷取建構的型別和泛型型別定義。The following code example creates an instance of a constructed type by using ordinary instance creation and then uses the GetType and GetGenericTypeDefinition methods to retrieve the constructed type and the generic type definition. 此範例中使用泛型Dictionary<TKey,TValue>型別,建構的型別代表Dictionary<TKey,TValue>Test具有字串索引鍵的物件。This example uses the generic Dictionary<TKey,TValue> type; the constructed type represents a Dictionary<TKey,TValue> of Test objects with string keys.

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
 */
Imports System
Imports System.Reflection
Imports System.Collections.Generic
Imports Microsoft.VisualBasic


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 'Main
    
    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 'DisplayTypeInfo
End Class 'Test

' 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
' 

備註

泛型類型定義是可從中建構其他類型的範本。A generic type definition is a template from which other types can be constructed. 例如,從泛型類型定義G<T>(以C#語法;G(Of T)在 Visual Basic 或generic <typename T> ref class G在C++) 您可以建構和具現化型別G<int>(G(Of Integer)在 Visual Basic 中)。For example, from the generic type definition G<T> (expressed in C# syntax; G(Of T) in Visual Basic or generic <typename T> ref class G in C++) you can construct and instantiate the type G<int> (G(Of Integer) in Visual Basic). 給定Type物件,代表這個建構的型別,GetGenericTypeDefinition方法會傳回泛型型別定義。Given a Type object representing this constructed type, the GetGenericTypeDefinition method returns the generic type definition.

如果兩個建構的型別會建立從相同的泛型類型定義中,使用相同的型別引數,GetGenericTypeDefinition方法會傳回相同Type這兩種類型的物件。If two constructed types are created from the same generic type definition, using the same type arguments, the GetGenericTypeDefinition method returns the same Type object for both types.

如果您呼叫GetGenericTypeDefinition方法Type物件,已表示泛型類型定義,它會傳回目前TypeIf you call the GetGenericTypeDefinition method on a Type object that already represents a generic type definition, it returns the current Type.

重要

泛型類型的陣列不是泛型本身。An array of generic types is not itself generic. 在 C# 程式碼A<int>[] v;或 Visual Basic 程式碼Dim v() As A(Of Integer),變數的型別v不是泛型。In the C# code A<int>[] v; or the Visual Basic code Dim v() As A(Of Integer), the type of variable v is not generic. 使用IsGenericType來判斷類型是否為泛型之前先呼叫GetGenericTypeDefinitionUse IsGenericType to determine whether a type is generic before calling GetGenericTypeDefinition.

如需泛型反映中所使用之規範的恆成立條件清單,請參閱 IsGenericType 屬性備註。For a list of the invariant conditions for terms used in generic reflection, see the IsGenericType property remarks.

適用於

另請參閱