英語で読む

次の方法で共有


Type.ContainsGenericParameters プロパティ

定義

現在の Type オブジェクトが特定の型で置き換えられていない型パラメーターを持っているかどうかを示す値を取得します。

C#
public virtual bool ContainsGenericParameters { get; }

プロパティ値

Boolean

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

次の例では、2 つの型パラメーターを持つジェネリック クラスを定義し、最初のクラスから派生する 2 番目のジェネリック クラスを定義します。 派生クラスの基本クラスには、2 つの型引数があります。1 つ目は であり、2 つ目は派生型の Int32 型パラメーターです。 この例では、 プロパティによって報告される位置など、これらのジェネリック クラスに関する情報が表示 GenericParameterPosition されます。

C#
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)
 */

注釈

型のインスタンスを作成するには、型自体の型引数、それを囲むジェネリック型、または型の任意の要素に、ジェネリック型定義またはオープン構築型が必要ではありません。 もう 1 つの言い方は、再帰的に検査する場合、型にジェネリック型パラメーターを含めなければならないという言い方です。

型は任意に複雑になる可能性があるから、この判断は困難です。 便宜上、およびエラーの可能性を減らすために、 プロパティは、インスタンス化できる閉じた構築型と、作成できないオープン構築型を区別するための標準的な方法を提供 ContainsGenericParameters します。 プロパティが ContainsGenericParameters を返す true 場合、型をインスタンス化できません。

プロパティ ContainsGenericParameters は、型パラメーターを再帰的に検索します。 たとえば、配列自体がジェネリックではない場合でも、要素が型 (Visual Basic) である配列に対して を true A<T> A(Of T) 返します。 これは、配列に対して を返 IsGenericType す プロパティの動作と false 対照的です。

一連のクラスの例と、 プロパティの値を示すテーブルについては、「」 ContainsGenericParameters を参照してください IsGenericType

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

こちらもご覧ください