ParameterInfo.GetCustomAttributes 메서드

정의

이 매개 변수에 적용된 사용자 지정 특성을 가져옵니다.

오버로드

GetCustomAttributes(Type, Boolean)

이 매개 변수에 적용되는 지정된 형식 또는 파생 형식의 사용자 지정 특성을 가져옵니다.

GetCustomAttributes(Boolean)

이 매개 변수에 정의된 모든 사용자 지정 특성을 가져옵니다.

GetCustomAttributes(Type, Boolean)

이 매개 변수에 적용되는 지정된 형식 또는 파생 형식의 사용자 지정 특성을 가져옵니다.

public:
 virtual cli::array <System::Object ^> ^ GetCustomAttributes(Type ^ attributeType, bool inherit);
public virtual object[] GetCustomAttributes (Type attributeType, bool inherit);
abstract member GetCustomAttributes : Type * bool -> obj[]
override this.GetCustomAttributes : Type * bool -> obj[]
Public Overridable Function GetCustomAttributes (attributeType As Type, inherit As Boolean) As Object()

매개 변수

attributeType
Type

형식으로 식별된 사용자 지정 특성입니다.

inherit
Boolean

이 인수는 이 형식의 개체에 대해 무시됩니다.

반환

Object[]

지정된 형식 또는 파생 형식의 사용자 지정 특성이 들어 있는 배열입니다.

구현

예외

형식은 기본 런타임 시스템에서 제공하는 형식이어야 합니다.

attributeType이(가) null인 경우

사용자 지정 특성 형식을 로드할 수 없는 경우

설명

이 메서드는 매개 변수를 inherit 무시합니다. 상속 체인에서 매개 변수의 특성을 검색하려면 메서드의 적절한 오버로드를 Attribute.GetCustomAttributes 사용합니다.

적용 대상

GetCustomAttributes(Boolean)

이 매개 변수에 정의된 모든 사용자 지정 특성을 가져옵니다.

public:
 virtual cli::array <System::Object ^> ^ GetCustomAttributes(bool inherit);
public virtual object[] GetCustomAttributes (bool inherit);
abstract member GetCustomAttributes : bool -> obj[]
override this.GetCustomAttributes : bool -> obj[]
Public Overridable Function GetCustomAttributes (inherit As Boolean) As Object()

매개 변수

inherit
Boolean

이 인수는 이 형식의 개체에 대해 무시됩니다.

반환

Object[]

이 매개 변수에 적용된 모든 사용자 지정 특성이 들어 있는 배열입니다.

구현

예외

사용자 지정 특성 형식을 로드할 수 없는 경우

예제

다음 예제에서는 메서드의 매개 변수에 적용된 사용자 지정 특성을 런타임에 검색하는 방법을 보여 줍니다. 이 예제에서는 매개 변수에 적용할 수 있는 라는 MyAttribute 사용자 지정 특성을 정의합니다. 그런 다음, 라는 메서드MyMethod를 사용하여 라는 MyClass 클래스를 정의하고 메서드의 매개 변수에 적용합니다MyAttribute.

예제가 실행되면 메서드를 GetCustomAttributes(Boolean) 사용하여 에 있는 모든 메서드의 모든 매개 변수에 MyClass적용된 사용자 지정 특성을 검색하고 콘솔에 표시합니다.

using namespace System;
using namespace System::Reflection;

// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets::Parameter)]
public ref class MyAttribute: public Attribute
{
private:
   String^ myName;

public:
   MyAttribute( String^ name )
   {
      myName = name;
   }

   property String^ Name 
   {
      String^ get()
      {
         return myName;
      }
   }
};

// Define a class which has a custom attribute associated with one of the 
// parameters of a method. 
public ref class MyClass1
{
public:
   void MyMethod( 
      [MyAttribute("This is an example parameter attribute")]
      int i ) {}
};

void main()
{
   // Get the type of the class 'MyClass1'.
   Type^ myType = MyClass1::typeid;

   // Get the members associated with the class 'MyClass1'.
   array<MethodInfo^>^myMethods = myType->GetMethods();

   // Display the attributes for each of the parameters of each method of the class 'MyClass1'.
   for ( int i = 0; i < myMethods->Length; i++ )
   {
      // Get the parameters for the method.
      array<ParameterInfo^>^myParameters = myMethods[ i ]->GetParameters();

      if ( myParameters->Length > 0 )
      {
         Console::WriteLine( "\nThe parameters for the method \"{0}\" that have custom attributes are:", myMethods[ i ] );
         for ( int j = 0; j < myParameters->Length; j++ )
         {
            // Get the attributes of type 'MyAttribute' for each parameter.
            array<Object^>^myAttributes = myParameters[ j ]->GetCustomAttributes( MyAttribute::typeid, false );

            if ( myAttributes->Length > 0 )
            {
               Console::WriteLine( "Parameter {0}, name = {1}, type = {2} has attributes:",
                  myParameters[ j ]->Position, 
                  myParameters[ j ]->Name, 
                  myParameters[ j ]->ParameterType );
               for ( int k = 0; k < myAttributes->Length; k++ )
               {
                  Console::WriteLine( "\t{0}", myAttributes[ k ] );
               }
            }
         }
      }
   }
}
/* This code example produces the following output:

The parameters for the method Void MyMethod(Int32) that have custom attributes are :
Parameter 0, name = i, type = System.Int32 has attributes:
        MyAttribute

The parameters for the method Boolean Equals(System.Object) that have custom attributes are :
 */
using System;
using System.Reflection;

// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets.Parameter)]
public class MyAttribute : Attribute
{
    private string myName;
    public MyAttribute(string name)
    {
        myName = name;
    }
    public string Name
    {
        get
        {
            return myName;
        }
    }
}

// Define a class which has a custom attribute associated with one of the
// parameters of a method.
public class MyClass1
{
    public void MyMethod(
        [MyAttribute("This is an example parameter attribute")]
        int i)
    {
        return;
    }
}

public class MemberInfo_GetCustomAttributes
{
    public static void Main()
    {
        // Get the type of the class 'MyClass1'.
        Type myType = typeof(MyClass1);
        // Get the members associated with the class 'MyClass1'.
        MethodInfo[] myMethods = myType.GetMethods();

        // Display the attributes for each of the parameters of each method of the class 'MyClass1'.
        for(int i = 0; i < myMethods.Length; i++)
        {
            // Get the parameters for the method.
            ParameterInfo[] myParameters = myMethods[i].GetParameters();

            if (myParameters.Length > 0)
            {
                Console.WriteLine("\nThe parameters for the method {0} that have custom attributes are :", myMethods[i]);
                for(int j = 0; j < myParameters.Length; j++)
                {
                    // Get the attributes of type 'MyAttribute' for each parameter.
                    Object[] myAttributes = myParameters[j].GetCustomAttributes(typeof(MyAttribute), false);

                    if (myAttributes.Length > 0)
                    {
                        Console.WriteLine("Parameter {0}, name = {1}, type = {2} has attributes: ",
                            myParameters[j].Position, myParameters[j].Name, myParameters[j].ParameterType);
                        for(int k = 0; k < myAttributes.Length; k++)
                        {
                            Console.WriteLine("\t{0}", myAttributes[k]);
                        }
                    }
                }
            }
        }
    }
}
/* This code example produces the following output:

The parameters for the method Void MyMethod(Int32) that have custom attributes are :
Parameter 0, name = i, type = System.Int32 has attributes:
        MyAttribute

The parameters for the method Boolean Equals(System.Object) that have custom attributes are :
 */
Imports System.Reflection

' Define a custom attribute with one named parameter.
<AttributeUsage(AttributeTargets.Parameter)> Public Class MyAttribute
    Inherits Attribute
    Private myName As String

    Public Sub New(ByVal name As String)
        myName = name
    End Sub 

    Public ReadOnly Property Name() As String
        Get
            Return myName
        End Get
    End Property
End Class 

' Define a class which has a custom attribute associated with one of 
' the parameters of a method. 
Public Class MyClass1

    Public Sub MyMethod( _
            <MyAttribute("This is an example parameter attribute")> _
            ByVal i As Integer _
        )
        Return
    End Sub 
End Class 


Public Class MemberInfo_GetCustomAttributes

    Public Shared Sub Main()
        ' Get the type of the class 'MyClass1'.
        Dim myType As Type = GetType(MyClass1)
        ' Get the members associated with the class 'MyClass1'.
        Dim myMethods As MethodInfo() = myType.GetMethods()

        ' Display the attributes for each of the parameters of each method of the class 'MyClass1'.
        For i As Integer = 0 To myMethods.Length - 1
            ' Get the parameters for the method.
            Dim myParameters As ParameterInfo() = myMethods(i).GetParameters()

            If myParameters.Length > 0 Then
                Console.WriteLine(vbCrLf & "The parameters for the method {0} that have custom attributes are : ", myMethods(i))
                For j As Integer = 0 To myParameters.Length - 1
                    ' Get the attributes of type 'MyAttribute' for each parameter.
                    Dim myAttributes As Object() = myParameters(j).GetCustomAttributes(GetType(MyAttribute), False)

                    If myAttributes.Length > 0 Then
                        Console.WriteLine("Parameter {0}, name = {1}, type = {2} has attributes: ", _
                            myParameters(j).Position, myParameters(j).Name, myParameters(j).ParameterType)
                        For k As Integer = 0 To myAttributes.Length - 1
                            Console.WriteLine(vbTab & "{0}", myAttributes(k))
                        Next k
                    End If
                Next j
            End If
        Next i
    End Sub 
End Class 

' This code example produces the following output:
'
'The parameters for the method Void MyMethod(Int32) that have custom attributes are :
'Parameter 0, name = i, type = System.Int32 has attributes:
'        MyAttribute
'
'The parameters for the method Boolean Equals(System.Object) that have custom attributes are :

설명

이 메서드는 매개 변수를 inherit 무시합니다. 상속 체인에서 매개 변수의 특성을 검색하려면 메서드의 적절한 오버로드를 Attribute.GetCustomAttributes 사용합니다.

적용 대상