ParameterInfo.GetCustomAttributes Method (Boolean)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets all the custom attributes defined on this parameter.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable Function GetCustomAttributes ( _
    inherit As Boolean _
) As Object()
public virtual Object[] GetCustomAttributes(
    bool inherit
)

Parameters

  • inherit
    Type: System.Boolean
    This parameter is ignored for objects of this type. See Remarks.

Return Value

Type: array<System.Object[]
An array that contains all the custom attributes applied to this parameter.

Implements

ICustomAttributeProvider.GetCustomAttributes(Boolean)

Exceptions

Exception Condition
TypeLoadException

A custom attribute type could not be loaded.

Remarks

This method ignores the inherit parameter. To search the inheritance chain for attributes on parameters, use the appropriate overloads of the Attribute.GetCustomAttributes method.

Examples

The following example shows how custom attributes that have been applied to the parameters of methods can be retrieved at run time. The example defines a custom attribute named MyAttribute that can be applied to parameters. The example then defines a class named MyClass with a method named MyMethod, and applies MyAttribute to a parameter of the method.

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.

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 Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' 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
            outputBlock.Text &= String.Format(vbCrLf & "The parameters for the method {0} that have custom attributes are : ", myMethods(i)) & vbCrLf
            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
                  outputBlock.Text &= String.Format("Parameter {0}, name = {1}, type = {2} has attributes: ", _
                      myParameters(j).Position, myParameters(j).Name, myParameters(j).ParameterType) & vbCrLf
                  For k As Integer = 0 To myAttributes.Length - 1
                     outputBlock.Text &= String.Format(vbTab & "{0}", myAttributes(k)) & vbCrLf
                  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 :
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 Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // 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)
         {
            outputBlock.Text += String.Format("\nThe parameters for the method {0} that have custom attributes are :", myMethods[i]) + "\n";
            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)
               {
                  outputBlock.Text += String.Format("Parameter {0}, name = {1}, type = {2} has attributes: ",
                      myParameters[j].Position, myParameters[j].Name, myParameters[j].ParameterType) + "\n";
                  for (int k = 0; k < myAttributes.Length; k++)
                  {
                     outputBlock.Text += String.Format("\t{0}", myAttributes[k]) + "\n";
                  }
               }
            }
         }
      }
   }
}
/* 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 :
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.