다음을 통해 공유


Type.GenericParameterAttributes 속성

정의

현재 제네릭 형식 매개 변수의 공변성 및 특수 제약 조건을 설명하는 GenericParameterAttributes 플래그의 조합을 가져옵니다.

public:
 virtual property System::Reflection::GenericParameterAttributes GenericParameterAttributes { System::Reflection::GenericParameterAttributes get(); };
public virtual System.Reflection.GenericParameterAttributes GenericParameterAttributes { get; }
member this.GenericParameterAttributes : System.Reflection.GenericParameterAttributes
Public Overridable ReadOnly Property GenericParameterAttributes As GenericParameterAttributes

속성 값

현재 제네릭 형식 매개 변수의 GenericParameterAttributes 공변성 및 특수 제약 조건을 설명하는 값의 비트 조합입니다.

예외

현재 Type 개체가 제네릭 형식 매개 변수가 아닙니다. 즉, 속성이 IsGenericParameter 반환됩니다 false.

호출된 메서드는 기본 클래스에서 지원되지 않습니다.

예제

다음 코드 예제에서는 제약 조건이 다른 두 형식 매개 변수를 사용하여 제네릭 Test 형식을 정의합니다. 프로그램이 실행되면 속성 및 메서드를 사용하여 GenericParameterAttributes 제약 조건을 검사합니다 GetGenericParameterConstraints .

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
 */
open System
open System.Reflection

// Define a sample interface to use as an interface constraint.
type ITest = interface end

// Define a base type to use as a base class constraint.
type Base() = class end

// 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.
type Test<'T, 'U
    when 'T :> Base 
    and 'T :> ITest
    and 'U : not struct 
    and 'U : (new : unit -> 'U)>() = class end

// Define a type that derives from Base and implements ITest. This type
// satisfies the constraints on T in class Test.
type Derived() = 
    inherit Base()
    interface ITest

// List the variance and special constraint flags. 
let listGenericParameterAttributes (t: Type) =
    let gpa = t.GenericParameterAttributes
    let variance = gpa &&& GenericParameterAttributes.VarianceMask

    let mutable retval = 
        // Select the variance flags.
        if variance = GenericParameterAttributes.None then
            "No variance flag"
        else
            if variance &&& GenericParameterAttributes.Covariant |> int <> 0 then
                "Covariant"
            else
                "Contravariant"
    // Select 
    let constraints = gpa &&& GenericParameterAttributes.SpecialConstraintMask

    if constraints = GenericParameterAttributes.None then
        retval <- retval + " No special constraints"
    else
        if constraints &&& GenericParameterAttributes.ReferenceTypeConstraint |> int <> 0 then
            retval <- retval + " ReferenceTypeConstraint"
        if constraints &&& GenericParameterAttributes.NotNullableValueTypeConstraint |> int <> 0 then
            retval <- retval + " NotNullableValueTypeConstraint"
        if constraints &&& GenericParameterAttributes.DefaultConstructorConstraint |> int <> 0 then
            retval <- retval + " DefaultConstructorConstraint"
    retval


// To get the generic type definition, call .GetGenericTypeDefinition().
let def = typeof<Test<Derived, _>>.GetGenericTypeDefinition()
printfn $"\nExamining generic type {def}"

// Get the type parameters of the generic type definition,
// and display them.
let defparams = def.GetGenericArguments()
for tp in defparams do
    printfn $"\nType parameter: {tp.Name}"
    printfn $"\t{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.
    let tpConstraints = tp.GetGenericParameterConstraints()
    for tpc in tpConstraints do
        printfn $"\t{tpc}"

(* 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
 *)
Imports System.Reflection

' Define a sample interface to use as an interface constraint.
Public Interface ITest
End Interface 

' Define a base type to use as a base class constraint.
Public Class Base
End Class 

' 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(Of T As {Base, ITest}, U As {New, Class}) 
End Class

' Define a type that derives from Base and implements ITtest. This type
' satisfies the constraints on T in class Test.
Public Class Derived
    Inherits Base
    Implements ITest
End Class 

Public Class Example
    
    Public Shared Sub Main() 
        ' To get the generic type definition, omit the type
        ' arguments but retain the comma to indicate the number
        ' of type arguments. 
        '
        Dim def As Type = GetType(Test(Of ,))
        Console.WriteLine(vbCrLf & "Examining generic type {0}", def)
        
        ' Get the type parameters of the generic type definition,
        ' and display them.
        '
        Dim defparams() As Type = def.GetGenericArguments()
        For Each tp As Type In defparams

            Console.WriteLine(vbCrLf & "Type parameter: {0}", tp.Name)
            Console.WriteLine(vbTab & ListGenericParameterAttributes(tp))
            
            ' List the base class and interface constraints. The
            ' constraints do not appear in any particular order. An
            ' empty array is returned if there are no constraints.
            '
            Dim tpConstraints As Type() = _
                tp.GetGenericParameterConstraints()
            For Each tpc As Type In  tpConstraints
                Console.WriteLine(vbTab & tpc.ToString())
            Next tpc
        Next tp
    
    End Sub 
    
    ' List the variance and special constraint flags.
    '
    Private Shared Function ListGenericParameterAttributes(ByVal t As Type) As String 
        Dim retval As String
        Dim gpa As GenericParameterAttributes = t.GenericParameterAttributes

        ' Select the variance flags.
        Dim variance As GenericParameterAttributes = _
            gpa And GenericParameterAttributes.VarianceMask
        
        If variance = GenericParameterAttributes.None Then
            retval = "No variance flag;"
        Else
            If (variance And GenericParameterAttributes.Covariant) <> 0 Then
                retval = "Covariant;"
            Else
                retval = "Contravariant;"
            End If
        End If 

        ' Select the constraint flags.
        Dim constraints As GenericParameterAttributes = _
            gpa And GenericParameterAttributes.SpecialConstraintMask
        
        If constraints = GenericParameterAttributes.None Then
            retval &= " no special constraints."
        Else
            If (constraints And GenericParameterAttributes.ReferenceTypeConstraint) <> 0 Then
                retval &= " ReferenceTypeConstraint"
            End If
            If (constraints And GenericParameterAttributes.NotNullableValueTypeConstraint) <> 0 Then
                retval &= " NotNullableValueTypeConstraint"
            End If
            If (constraints And GenericParameterAttributes.DefaultConstructorConstraint) <> 0 Then
                retval &= " DefaultConstructorConstraint"
            End If
        End If 
        Return retval
    
    End Function 
End Class 
' 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.VarianceMask 사용하여 공변성 플래그를 선택하고 값을 사용하여 GenericParameterAttributes.SpecialConstraintMask 제약 조건 플래그를 선택합니다.

제네릭 리플렉션에 사용되는 용어에 대한 고정 조건 목록은 속성 비고를 IsGenericType 참조하세요.

적용 대상

추가 정보