此主题尚未评级 - 评价此主题

Type.GenericParameterAttributes 属性

2013/12/13

获取描述当前泛型类型参数的协变和特殊约束的 GenericParameterAttributes 标志。

Namespace:  System
程序集:  mscorlib(位于 mscorlib.dll 中)
public virtual GenericParameterAttributes GenericParameterAttributes { get; }

属性值

类型: System.Reflection.GenericParameterAttributes
GenericParameterAttributes 值的按位组合,用于描述当前泛型类型参数的协变和特殊约束。
异常条件
InvalidOperationException

当前的 Type 对象不是泛型类型参数。即,IsGenericParameter 属性返回 false

NotSupportedException

基类不支持所调用的方法。

此属性的值包含了描述当前泛型类型参数是否为协变的标志以及描述任何特殊约束的标志。使用 GenericParameterAttributes.VarianceMask 值可以选择协变标志,使用 GenericParameterAttributes.SpecialConstraintMask 值可以选择约束标志。

有关泛型反射中使用的术语的固定条件列表,请参见 IsGenericType 属性备注。

下面的代码示例定义泛型类型 Test,该泛型类型包含两个具有不同约束的类型参数。当程序执行时,使用 GenericParameterAttributes 属性和 GetGenericParameterConstraints 方法检查这些约束。

说明注意:

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


using System;
using System.Reflection;

// Define a sample interface to use as an interface constraint.
public interface ITest { }

// Define a base type to use as a base class constraint.
public class Base { }

// Define the generic type to examine. The first generic type parameter,
// T, derives from the class Base and implements ITest. This demonstrates
// a base class constraint and an interface constraint. The second generic 
// type parameter, U, must be a reference type (class) and must have a 
// default constructor (new()). This demonstrates special constraints.
//
public class Test<T, U>
   where T : Base, ITest
   where U : class, new() { }

// Define a type that derives from Base and implements ITest. This type
// satisfies the constraints on T in class Test.
public class Derived : Base, ITest { }

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // To get the generic type definition, omit the type
      // arguments but retain the comma to indicate the number
      // of type arguments. 
      //
      Type def = typeof(Test<,>);
      outputBlock.Text += String.Format("\r\nExamining generic type {0}", def) + "\n";

      // Get the type parameters of the generic type definition,
      // and display them.
      //
      Type[] defparams = def.GetGenericArguments();
      foreach (Type tp in defparams)
      {
         outputBlock.Text += String.Format("\r\nType parameter: {0}", tp.Name) + "\n";
         outputBlock.Text += String.Format("\t{0}",
             ListGenericParameterAttributes(tp)) + "\n";

         // List the base class and interface constraints. The
         // constraints are returned in no particular order. If 
         // there are no class or interface constraints, an empty
         // array is returned.
         //
         Type[] tpConstraints = tp.GetGenericParameterConstraints();
         foreach (Type tpc in tpConstraints)
         {
            outputBlock.Text += String.Format("\t{0}", tpc) + "\n";
         }
      }
   }

   // List the variance and special constraint flags. 
   //
   private static string ListGenericParameterAttributes(Type t)
   {
      string retval;
      GenericParameterAttributes gpa = t.GenericParameterAttributes;
      GenericParameterAttributes variance = gpa &
          GenericParameterAttributes.VarianceMask;

      // Select the variance flags.
      if (variance == GenericParameterAttributes.None)
         retval = "No variance flag;";
      else
      {
         if ((variance & GenericParameterAttributes.Covariant) != 0)
            retval = "Covariant;";
         else
            retval = "Contravariant;";
      }

      // Select 
      GenericParameterAttributes constraints = gpa &
          GenericParameterAttributes.SpecialConstraintMask;

      if (constraints == GenericParameterAttributes.None)
         retval += " No special constraints";
      else
      {
         if ((constraints & GenericParameterAttributes.ReferenceTypeConstraint) != 0)
            retval += " ReferenceTypeConstraint";
         if ((constraints & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
            retval += " NotNullableValueTypeConstraint";
         if ((constraints & GenericParameterAttributes.DefaultConstructorConstraint) != 0)
            retval += " DefaultConstructorConstraint";
      }

      return retval;
   }
}
/* This example produces the following output:

Examining generic type Test`2[T,U]

Type parameter: T
        No variance flag; no special constraints.
        Base
        ITest

Type parameter: U
        No variance flag; ReferenceTypeConstraint DefaultConstructorConstraint
 */


Windows Phone OS

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

Windows Phone

本文是否对您有所帮助?
(1500 个剩余字符)
感谢您的反馈
显示:
© 2014 Microsoft. 版权所有。