GenericParameterAttributes 枚举

定义

描述泛型类型或方法的泛型类型参数的约束。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum GenericParameterAttributes
继承
GenericParameterAttributes
属性

字段

名称 说明
AllowByRefLike 32

泛型类型参数可以 ByRefLike

Contravariant 2

泛型类型参数是逆变的。 逆变类型参数可以在方法签名中显示为参数类型。

Covariant 1

泛型类型参数是协变的。 协变类型参数可以显示为方法的结果类型、只读字段的类型、声明的基类型或实现的接口。

DefaultConstructorConstraint 16

仅当类型具有无参数构造函数时,才能将类型替换为泛型类型参数。

None 0

没有特殊标志。

NotNullableValueTypeConstraint 8

仅当类型为值类型且不可为 null 时,才能将类型替换为泛型类型参数。

ReferenceTypeConstraint 4

仅当类型为引用类型时,才能将类型替换为泛型类型参数。

SpecialConstraintMask 28

选择所有特殊约束标志的组合。 此值是使用逻辑 OR 合并以下标志的结果:DefaultConstructorConstraintReferenceTypeConstraintNotNullableValueTypeConstraint

VarianceMask 3

选择所有方差标志的组合。 此值是使用逻辑 OR 合并以下标志的结果:ContravariantCovariant

示例

下面的代码示例使用两个类型参数定义泛型类型 Test。 第二个类型参数具有基类约束和引用类型约束。 执行程序时,将使用 Type.GenericParameterAttributes 属性和 Type.GetGenericParameterConstraints 方法检查约束。

C#
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 Main()
    {
        // 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<,>);
        Console.WriteLine("\r\nExamining generic type {0}", def);

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

            // 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)
            {
                Console.WriteLine("\t{0}", tpc);
            }
        }
    }

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

注解

GenericParameterAttributes 枚举的成员分为两个组:方差组和特殊约束组。 若要测试方差标志的 GenericParameterAttributes 值,请先使用 VarianceMask 执行按位 AND 操作。 如果结果为 None,则没有方差标志。 同样,使用 SpecialConstraintMask 测试约束标志。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0