Type.GetGenericTypeDefinition メソッド

定義

現在のジェネリック型を構築する元になるジェネリック型定義を表す 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. つまり、IsGenericTypefalse を返します。That 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.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
' 

注釈

ジェネリック型定義は、他の型を構築できるテンプレートです。A generic type definition is a template from which other types can be constructed. たとえば、ジェネリック型の定義G<T> (構文でC#表されます。G<int> G(Of Integer) Visual Basic またはgeneric <typename T> ref class GのC++) では、型を構築およびインスタンス化できます (Visual Basic)。 G(Of T)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.

同じ型引数を使用して、同じジェネリック型定義から2つの構築型が作成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ているオブジェクトに対してメソッドを呼び出すと、現在Typeのが返されます。If 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#コードDim v() As A(Of Integer) vまたは Visual Basic コードでは、変数の型はジェネリックではありません。 A<int>[] 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 呼び出すGetGenericTypeDefinition前に、を使用して、型がジェネリックであるかどうかを判断します。Use 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.

適用対象

こちらもご覧ください