Type.GetGenericTypeDefinition Metoda

Definicja

Zwraca obiekt reprezentujący definicję typu ogólnego, z której można utworzyć Type bieżący typ ogólny.

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

Zwraca

Type

Obiekt Type reprezentujący typ ogólny, z którego można utworzyć bieżący typ.

Wyjątki

Bieżący typ nie jest typem ogólnym. Oznacza to, że IsGenericType zwraca wartość false .

Wywoływana metoda nie jest obsługiwana w klasie bazowej. Klasy pochodne muszą zapewniać implementację.

Przykłady

Poniższy przykład kodu tworzy wystąpienie skonstruowanego typu przy użyciu zwykłego tworzenia wystąpienia, a następnie używa metod i do pobrania skonstruowanego typu i definicji GetType GetGenericTypeDefinition typu ogólnego. W tym przykładzie użyto typu ogólnego, a Dictionary<TKey,TValue> skonstruowany typ reprezentuje obiekt Dictionary<TKey,TValue> z Test kluczami ciągów.

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

Uwagi

Definicja typu ogólnego to szablon, na podstawie którego można utworzyć inne typy. Na przykład z definicji typu ogólnego (wyrażonej w składni języka C# w języku Visual Basic lub C++) można utworzyć i utworzyć wystąpienia typu ( w języku G<T> G(Of T) generic <typename T> ref class G G<int> G(Of Integer) Visual Basic). Mając obiekt Type reprezentujący ten skonstruowany typ, GetGenericTypeDefinition metoda zwraca definicję typu ogólnego.

Jeśli dwa skonstruowane typy są tworzone na podstawie tej samej definicji typu ogólnego, używając tych samych argumentów typu, metoda zwraca ten GetGenericTypeDefinition sam obiekt dla obu Type typów.

Jeśli metoda zostanie GetGenericTypeDefinition wywołana na Type obiekcie, który już reprezentuje definicję typu ogólnego, zwraca bieżącą wartość Type .

Ważne

Tablica typów ogólnych nie jest sama w sobie ogólną. W kodzie języka C# A<int>[] v; lub Visual Basic typ zmiennej nie jest Dim v() As A(Of Integer) v ogólny. Użyj IsGenericType metody , aby określić, czy typ jest ogólny przed wywołaniem metody GetGenericTypeDefinition .

Aby uzyskać listę niezmiennych warunków dla terminów używanych w ogólnym odbicia, zobacz IsGenericType uwagi dotyczące właściwości.

Dotyczy

Zobacz też