Type.ContainsGenericParameters プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)

Type オブジェクトが、特定の型が指定されていない型パラメータを持っている場合は true。そうでない場合は false。

型のインスタンスを作成する場合は、包含するジェネリック型の中、または型の要素の中で、型そのものの型引数にジェネリック型定義またはオープン構築型が存在しないことが必須条件です。つまり、再帰的に調べたときに、型にジェネリック型パラメータが含まれていることは許容されません。
型が複雑になると、判断が困難な場合があります。ContainsGenericParameters プロパティは、インスタンス化できるクローズ構築型とインスタンス化できないオープン構築型を区別する標準的な方法を提供します。これは便利な方法であり、エラーの可能性も低くなります。ContainsGenericParameters プロパティが true を返す場合、型はインスタンス化できません。
ContainsGenericParameters プロパティは、型パラメータを再帰的に検索します。たとえば、要素の型が A<T> (Visual Basic の場合は A(Of T)) である配列に対して、配列そのものがジェネリック型でなくても true を返します。これは、配列に対して false を返す IsGenericType プロパティの動作とは異なります。
ContainsGenericParameters プロパティを示すクラスの例および表については、IsGenericType のトピックを参照してください。

2 つの型パラメータを指定したジェネリック クラス、および最初のクラスから派生する 2 番目のジェネリック クラスを定義する例を次に示します。派生クラスの基本クラスには 2 つの型引数があります。最初の型引数は Int32 で、2 番目の型引数は派生型の型パラメータです。この例では、GenericParameterPosition プロパティによって報告された位置を含む、ジェネリック クラスの情報が示されています。
Imports System Imports System.Reflection Imports System.Collections.Generic Imports Microsoft.VisualBasic ' Define a base class with two type parameters. Public Class Base(Of T, U) End Class ' Define a derived class. The derived class inherits from a constructed ' class that meets the following criteria: ' (1) Its generic type definition is Base<T, U>. ' (2) It uses int for the first type parameter. ' (3) For the second type parameter, it uses the same type that is used ' for the type parameter of the derived class. ' Thus, the derived class is a generic type with one type parameter, but ' its base class is an open constructed type with one assigned type ' parameter and one unassigned type parameter. Public Class Derived(Of V) Inherits Base(Of Integer, V) End Class Public Class Test Public Shared Sub Main() Console.WriteLine(vbCrLf _ & "--- Display a generic type and the open constructed") Console.WriteLine(" type from which it is derived.") ' Create a Type object representing the generic type definition ' for the Derived type, by omitting the type argument. (For ' types with multiple type parameters, supply the commas but ' omit the type arguments.) ' Dim derivedType As Type = GetType(Derived(Of )) DisplayGenericTypeInfo(derivedType) ' Display its open constructed base type. DisplayGenericTypeInfo(derivedType.BaseType) End Sub 'Main Private Shared Sub DisplayGenericTypeInfo(ByVal t As Type) Console.WriteLine(vbCrLf & "{0}", t) Console.WriteLine(vbTab & "Is this a generic type definition? " _ & t.IsGenericTypeDefinition) Console.WriteLine(vbTab & "Is it a generic type? " _ & t.IsGenericType) Console.WriteLine(vbTab _ & "Does it have unassigned generic parameters? " _ & t.ContainsGenericParameters) 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 ' IsGenericParameter is true only for generic type ' parameters. ' If tParam.IsGenericParameter Then Console.WriteLine(vbTab & vbTab & tParam.ToString() _ & " (unassigned - parameter position " _ & tParam.GenericParameterPosition & ")") Else Console.WriteLine(vbTab & vbTab & tParam.ToString()) End If Next tParam End If End Sub 'DisplayGenericTypeInfo End Class 'Test ' This example produces the following output: ' '--- Display a generic type and the open constructed ' type from which it is derived. ' 'Derived`1[V] ' Is this a generic type definition? True ' Is it a generic type? True ' Does it have unassigned generic parameters? True ' List type arguments (1): ' V (unassigned - parameter position 0) ' 'Base`2[System.Int32,V] ' Is this a generic type definition? False ' Is it a generic type? True ' Does it have unassigned generic parameters? True ' List type parameters (2): ' System.Int32 ' V (unassigned - parameter position 0) '
using System; using System.Reflection; using System.Collections.Generic; // Define a base class with two type parameters. public class Base<T, U> { } // Define a derived class. The derived class inherits from a constructed // class that meets the following criteria: // (1) Its generic type definition is Base<T, U>. // (2) It specifies int for the first type parameter. // (3) For the second type parameter, it uses the same type that is used // for the type parameter of the derived class. // Thus, the derived class is a generic type with one type parameter, but // its base class is an open constructed type with one type argument and // one type parameter. public class Derived<V> : Base<int, V> { } public class Test { public static void Main() { Console.WriteLine( "\r\n--- Display a generic type and the open constructed"); Console.WriteLine(" type from which it is derived."); // Create a Type object representing the generic type definition // for the Derived type, by omitting the type argument. (For // types with multiple type parameters, supply the commas but // omit the type arguments.) // Type derivedType = typeof(Derived<>); DisplayGenericTypeInfo(derivedType); // Display its open constructed base type. DisplayGenericTypeInfo(derivedType.BaseType); } 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); Console.WriteLine("\tDoes it have unassigned generic parameters? {0}", t.ContainsGenericParameters); 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) { // IsGenericParameter is true only for generic type // parameters. // if (tParam.IsGenericParameter) { Console.WriteLine( "\t\t{0} (unassigned - parameter position {1})" , tParam, tParam.GenericParameterPosition); } else { Console.WriteLine("\t\t{0}", tParam); } } } } } /* This example produces the following output: --- Display a generic type and the open constructed type from which it is derived. Derived`1[V] Is this a generic type definition? True Is it a generic type? True Does it have unassigned generic parameters? True List type arguments (1): V (unassigned - parameter position 0) Base`2[System.Int32,V] Is this a generic type definition? False Is it a generic type? True Does it have unassigned generic parameters? True List type arguments (2): System.Int32 V (unassigned - parameter position 0) */
using namespace System; using namespace System::Reflection; using namespace System::Collections::Generic; // Define a base class with two type parameters. generic< class T,class U > public ref class Base {}; // Define a derived class. The derived class inherits from a constructed // class that meets the following criteria: // (1) Its generic type definition is Base<T, U>. // (2) It specifies int for the first type parameter. // (3) For the second type parameter, it uses the same type that is used // for the type parameter of the derived class. // Thus, the derived class is a generic type with one type parameter, but // its base class is an open constructed type with one type argument and // one type parameter. generic<class V> public ref class Derived : Base<int,V> {}; public ref class Test { public: static void Main() { Console::WriteLine( L"\r\n--- Display a generic type and the open constructed"); Console::WriteLine(L" type from which it is derived."); // Create a Type object representing the generic type definition // for the Derived type. Note the absence of type arguments. // Type^ derivedType = Derived::typeid; DisplayGenericTypeInfo(derivedType); // Display its open constructed base type. DisplayGenericTypeInfo(derivedType->BaseType); } 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); Console::WriteLine(L"\tDoes it have unassigned generic parameters? {0}" , t->ContainsGenericParameters); 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); // IsGenericParameter is true only for generic type // parameters. // if (tParam->IsGenericParameter) { Console::WriteLine( L"\t\t{0} (unassigned - parameter position {1})", tParam, tParam->GenericParameterPosition); } else { Console::WriteLine(L"\t\t{0}", tParam); } } } } }; int main() { Test::Main(); } /* This example produces the following output: --- Display a generic type and the open constructed type from which it is derived. Derived`1[V] Is this a generic type definition? True Is it a generic type? True Does it have unassigned generic parameters? True List type arguments (1): V (unassigned - parameter position 0) Base`2[System.Int32,V] Is this a generic type definition? False Is it a generic type? True Does it have unassigned generic parameters? True List type arguments (2): System.Int32 V (unassigned - parameter position 0) */

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.ContainsGenericParameters プロパティを検索する場合は、下記のリンクをクリックしてください。

- Type.ContainsGenericParameters プロパティのページへのリンク