信息
您所需的主题如下所示。但此主题未包含在此库中。

Type.ContainsGenericParameters 属性

2013/12/13

获取一个值,该值指示当前 Type 对象是否具有尚未被特定类型替代的类型参数。

Namespace:  System
程序集:  mscorlib(位于 mscorlib.dll 中)

public virtual bool ContainsGenericParameters { get; }

属性值

类型: System.Boolean
如果 Type 对象本身是泛型类型形参或者具有尚未提供特定类型的类型形参,则为 true;否则为 false

为了创建某个类型的实例,该类型本身的类型实参、任何封闭泛型类型或该类型的任何元素中一定不能存在泛型类型定义或开放构造类型。对此的另一种表述是:在递归地进行检查时,该类型一定不能包含任何泛型类型参数。

由于类型可以任意复杂,要确定这点是很困难的。为了方便起见和减小出错的可能性,ContainsGenericParameters 属性提供了一种标准方法来区分封闭构造类型(可以实例化)和开放构造类型(不能实例化)。如果 ContainsGenericParameters 属性返回 true,则该类型不能实例化。

ContainsGenericParameters 属性递归地搜索类型参数。例如,对于其元素为类型 A<T>(在 Visual Basic 中为 A(Of T))的数组,它将返回 true,虽然该数组本身不是泛型。IsGenericType 属性的行为与此形成对比,它对数组返回 false

有关示例类集合和显示 ContainsGenericParameters 属性值的表,请参见 IsGenericType

下面的示例定义具有两个类型参数的泛型类,然后定义从第一个类派生的第二个泛型类。派生类的基类有两个类型参数:第一个参数为 Int32,第二个参数为派生类型的类型参数。该示例显示有关这些泛型类的信息,其中包括 GenericParameterPosition 属性报告的位置。

说明注意:

要运行此示例,请参见生成具有静态 Windows Phone TextBlock 控件的示例


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 Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text +=
          "\r\n--- Display a generic type and the open constructed" + "\n";
      outputBlock.Text += "    type from which it is derived." + "\n";

      // 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(outputBlock, derivedType);

      // Display its open constructed base type.
      DisplayGenericTypeInfo(outputBlock, derivedType.BaseType);
   }

   private static void DisplayGenericTypeInfo(System.Windows.Controls.TextBlock outputBlock, Type t)
   {
      outputBlock.Text += String.Format("\r\n{0}", t) + "\n";

      outputBlock.Text += String.Format("\tIs this a generic type definition? {0}",
          t.IsGenericTypeDefinition) + "\n";

      outputBlock.Text += String.Format("\tIs it a generic type? {0}",
          t.IsGenericType) + "\n";

      outputBlock.Text += String.Format("\tDoes it have unassigned generic parameters? {0}",
          t.ContainsGenericParameters) + "\n";

      if (t.IsGenericType)
      {
         // If this is a generic type, display the type arguments.
         //
         Type[] typeArguments = t.GetGenericArguments();

         outputBlock.Text += String.Format("\tList type arguments ({0}):",
             typeArguments.Length) + "\n";

         foreach (Type tParam in typeArguments)
         {
            // IsGenericParameter is true only for generic type
            // parameters.
            //
            if (tParam.IsGenericParameter)
            {
               outputBlock.Text += String.Format(
                   "\t\t{0}  (unassigned - parameter position {1})",
                   tParam,
                   tParam.GenericParameterPosition) + "\n";
            }
            else
            {
               outputBlock.Text += String.Format("\t\t{0}", tParam) + "\n";
            }
         }
      }
   }
}

/* 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 Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示: