ParameterInfo.GetCustomAttributes 方法

定義

取得套用至這個參數的自訂屬性。Gets the custom attributes applied to this parameter.

多載

GetCustomAttributes(Type, Boolean)

取得套用至此參數之指定型別及其衍生型別的自訂屬性。Gets the custom attributes of the specified type or its derived types that are applied to this parameter.

GetCustomAttributes(Boolean)

取得在這個參數上所定義的所有自訂屬性。Gets all the custom attributes defined on this parameter.

GetCustomAttributes(Type, Boolean)

取得套用至此參數之指定型別及其衍生型別的自訂屬性。Gets the custom attributes of the specified type or its derived types that are applied to this parameter.

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

依型別識別的自訂屬性。The custom attributes identified by type.

inherit
Boolean

這個類型的物件會忽略這個引數。This argument is ignored for objects of this type.

傳回

Object[]

包含指定型別及其衍生型別之自訂屬性的陣列。An array that contains the custom attributes of the specified type or its derived types.

實作

例外狀況

類型必須是基礎執行階段系統所提供的類型。The type must be a type provided by the underlying runtime system.

attributeTypenullattributeType is null.

無法載入自訂屬性型別。A custom attribute type could not be loaded.

備註

這個方法會忽略 inherit 參數。This method ignores the inherit parameter. 若要在繼承鏈中搜尋參數上的屬性,請使用方法的適當多載 Attribute.GetCustomAttributesTo search the inheritance chain for attributes on parameters, use the appropriate overloads of the Attribute.GetCustomAttributes method.

適用於

GetCustomAttributes(Boolean)

取得在這個參數上所定義的所有自訂屬性。Gets all the custom attributes defined on this parameter.

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

這個類型的物件會忽略這個引數。This argument is ignored for objects of this type.

傳回

Object[]

包含套用至此參數之所有自訂屬性的陣列。An array that contains all the custom attributes applied to this parameter.

實作

例外狀況

無法載入自訂屬性型別。A custom attribute type could not be loaded.

範例

下列範例會顯示如何在執行時間中,抓取已套用至方法參數的自訂屬性。The following example shows how custom attributes that have been applied to the parameters of methods can be retrieved at run time. 此範例會定義名為的自訂屬性 MyAttribute ,該屬性可以套用至參數。The example defines a custom attribute named MyAttribute that can be applied to parameters. 然後,此範例會使用名為的方法定義名為的類別 MyClass MyMethod ,並套用 MyAttribute 至方法的參數。The example then defines a class named MyClass with a method named MyMethod, and applies MyAttribute to a parameter of the method.

執行此範例時,它會使用方法來抓取已套用 GetCustomAttributes(Boolean) 至所有方法之所有參數的自訂屬性 MyClass ,並在主控台中顯示這些屬性。When the example is run, it uses the GetCustomAttributes(Boolean) method to retrieve the custom attributes that have been applied to all parameters of all methods in MyClass, and displays them at the console.

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 參數。This method ignores the inherit parameter. 若要在繼承鏈中搜尋參數上的屬性,請使用方法的適當多載 Attribute.GetCustomAttributesTo search the inheritance chain for attributes on parameters, use the appropriate overloads of the Attribute.GetCustomAttributes method.

適用於