Type.GetGenericTypeDefinition メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As Type Dim returnValue As Type returnValue = instance.GetGenericTypeDefinition
現在の型を構築する元になるジェネリック型を表す Type オブジェクト。


ジェネリック型定義は、他の型を構築するためのテンプレートです。たとえば、ジェネリック型定義 G<T> (C# 構文の場合) (Visual Basic の場合は G(Of T)、C++ の場合は generic <typename T> ref class G) から型 G<int> (Visual Basic の場合は G(Of Integer)) を構築およびインスタンス化できます。構築型を表す Type オブジェクトの場合、GetGenericTypeDefinition メソッドはジェネリック型定義を返します。
2 つの構築型が同じジェネリック型定義から同じ型引数を使って作成された場合、GetGenericTypeDefinition メソッドは両方の型に対して同じ Type オブジェクトを返します。
既にジェネリック型定義を表している Type オブジェクトで GetGenericTypeDefinition メソッドを呼び出した場合、現在の Type が返されます。
![]() |
---|
ジェネリック型の配列はジェネリック型ではありません。C# コード A<int>[] v;、または Visual Basic コード Dim v() As A(Of Integer) で、変数 v はジェネリック型ではありません。GetGenericTypeDefinition を呼び出す前に、IsGenericType を使用して型がジェネリックであるかどうかを判断します。 |
ジェネリック リフレクションで使用される用語の一定の条件の一覧については、IsGenericType のプロパティの解説を参照してください。

通常のインスタンス作成を使用して構築型のインスタンスを作成してから、GetType メソッドおよび GetGenericTypeDefinition メソッドを使用して構築型およびジェネリック型の定義を取得するコード例を次に示します。この例では、ジェネリック型 Dictionary を使用します。構築型は、文字列キーを使用して Test オブジェクトの Dictionary を表します。
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 '
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 */
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 */
import System.*; import System.Reflection.*; import System.Collections.Generic.*; public class Test { public static void main(String[] args) { 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); } //main private static void DisplayTypeInfo(Type t) { Console.WriteLine("\r\n{0}", t); Console.WriteLine("\tIs this a generic type definition? {0}", (System.Boolean)t.get_IsGenericTypeDefinition()); Console.WriteLine("\tIs it a generic type? {0}", (System.Boolean)t.get_IsGenericType()); Type typeArguments[] = t.GetGenericArguments(); Console.WriteLine("\tList type arguments ({0}):", (Int32)typeArguments.get_Length()); for (int iCtr = 0; iCtr < typeArguments.get_Length(); iCtr++) { Type tParam = (Type)typeArguments.get_Item(iCtr); Console.WriteLine("\t\t{0}", tParam); } } //DisplayTypeInfo } //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 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からType.GetGenericTypeDefinition メソッドを検索する場合は、下記のリンクをクリックしてください。

- Type.GetGenericTypeDefinition メソッドのページへのリンク