Type.IsGenericTypeDefinition Eigenschaft

Definition

Ruft einen Wert ab, der angibt, ob der aktuelle Type eine generische Typdefinition darstellt, aus der andere generische Typen konstruiert werden können.

public:
 virtual property bool IsGenericTypeDefinition { bool get(); };
public virtual bool IsGenericTypeDefinition { get; }
member this.IsGenericTypeDefinition : bool
Public Overridable ReadOnly Property IsGenericTypeDefinition As Boolean

Eigenschaftswert

true, wenn das Type-Objekt eine generische Typdefinition darstellt, andernfalls false.

Beispiele

Im folgenden Beispiel werden Informationen zu einem Typ angezeigt, einschließlich der Angabe, ob es sich um eine generische Typdefinition handelt. Informationen werden für einen konstruierten Typ, für seine generische Typdefinition und für einen gewöhnlichen Typ angezeigt.

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

public ref class Test
{
private:
   static void DisplayGenericTypeInfo( 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 );
      
      if ( t->IsGenericType )
      {
         
         // If this is a generic type, display the type arguments.
         //
         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);
            
            // If this is a type parameter, display its
            // position.
            //
            if ( tParam->IsGenericParameter )
            {
               Console::WriteLine(
                  L"\t\t{0}\t(unassigned - parameter position {1})",
                  tParam, tParam->GenericParameterPosition );
            }
            else
            {
               Console::WriteLine( L"\t\t{0}", tParam );
            }
         }
      }
   }


public:
   static void Main()
   {
      Console::Write( L"\r\n--- Display information about a " );
      Console::WriteLine( L"constructed type, its" );
      Console::WriteLine( L"    generic type definition, and an ordinary type." );
      
      // Create a Dictionary of Test objects, using strings for the
      // keys.
      Dictionary< String^,Test^ >^ d = gcnew 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( String::typeid );
   }

};

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

/* 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
 */
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
 */
open System
open System.Collections.Generic

type Test() = class end

let displayGenericTypeInfo (t: Type) =
    printfn $"\n{t}"
    printfn $"\tIs this a generic type definition? {t.IsGenericTypeDefinition}" 
    printfn $"\tIs it a generic type? {t.IsGenericType}" 

    if t.IsGenericType then
        // If this is a generic type, display the type arguments.
        let typeArguments = t.GetGenericArguments()

        printfn $"\tList type arguments ({typeArguments.Length}):"

        for tParam in typeArguments do
            // If this is a type parameter, display its
            // position.
            if tParam.IsGenericParameter then
                printfn $"\t\t{tParam}\t(unassigned - parameter position {tParam.GenericParameterPosition})"
            else
                printfn $"\t\t{tParam}"

printfn "\r\n--- Display information about a constructed type, its"
printfn "    generic type definition, and an ordinary type."

// Create a Dictionary of Test objects, using strings for the keys.       
let d = 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
 *)
Imports System.Reflection
Imports System.Collections.Generic

Public Class Test
    Private Shared Sub DisplayGenericTypeInfo(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)
        
        If t.IsGenericType Then
            ' If this is a generic type, display the type arguments.
            '
            Dim typeArguments As Type() = t.GetGenericArguments()
            
            Console.WriteLine(vbTab & "List type arguments (" _
                & typeArguments.Length & "):")
            
            For Each tParam As Type In typeArguments
                ' If this is a type parameter, display its position.
                '
                If tParam.IsGenericParameter Then
                    Console.WriteLine(vbTab & vbTab & tParam.ToString() _
                        & vbTab & "(unassigned - parameter position " _
                        & tParam.GenericParameterPosition & ")")
                Else
                    Console.WriteLine(vbTab & vbTab & tParam.ToString())
                End If
            Next tParam
        End If
    
    End Sub 
    
    
    Public Shared Sub Main() 
        Console.WriteLine(vbCrLf & "--- 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.       
        Dim d As New Dictionary(Of String, Test)()

        DisplayGenericTypeInfo(d.GetType())
        DisplayGenericTypeInfo(d.GetType().GetGenericTypeDefinition())
        
        ' Display information for an ordinary type.
        DisplayGenericTypeInfo(GetType(String))
    
    End Sub
End Class

' 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

Hinweise

Eine generische Typdefinition ist eine Vorlage, aus der andere Typen erstellt werden können. Beispielsweise können Sie aus der generischen Typdefinition G<T> (ausgedrückt in C#-Syntax; G(Of T) in Visual Basic oder generic <typename T> ref class G in C++) den Typ G<int> (G(Of Integer) in Visual Basic) konstruieren und instanziieren, indem Sie die MakeGenericType -Methode mit einer generischen Argumentliste aufrufen, die den Int32 Typ enthält. Bei einem Type Objekt, das diesen konstruierten Typ darstellt, ruft die GetGenericTypeDefinition Methode die generische Typdefinition wieder zurück.

Verwenden Sie die IsGenericTypeDefinition -Eigenschaft, um zu bestimmen, ob Sie neue Typen aus dem aktuellen Typ erstellen können. Wenn die IsGenericTypeDefinition -Eigenschaft zurückgibt true, können Sie die MakeGenericType -Methode aufrufen, um neue generische Typen zu erstellen.

Eine Liste der invarianten Bedingungen für Begriffe, für Begriffe, die für die Reflektion mit generischen Methoden verwendet werden, finden Sie in den Hinweisen zur Eigenschaft IsGenericType.

Gilt für:

Weitere Informationen