.NET Framework Class Library
AttributeGetCustomAttributes Method (ParameterInfo, Boolean)

Retrieves an array of the custom attributes applied to a method parameter. Parameters specify the method parameter, and whether to search ancestors of the method parameter.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Shared Function GetCustomAttributes ( _
	element As [%$TOPIC/2fw1se54_en-us_VS_110_1_0_0_0_0%], _
	inherit As [%$TOPIC/2fw1se54_en-us_VS_110_1_0_0_0_1%] _
) As [%$TOPIC/2fw1se54_en-us_VS_110_1_0_0_0_2%]()
public static [%$TOPIC/2fw1se54_en-us_VS_110_1_0_1_0_0%][] GetCustomAttributes(
	[%$TOPIC/2fw1se54_en-us_VS_110_1_0_1_0_1%] element,
	[%$TOPIC/2fw1se54_en-us_VS_110_1_0_1_0_2%] inherit
)
public:
static array<[%$TOPIC/2fw1se54_en-us_VS_110_1_0_2_0_0%]^>^ GetCustomAttributes(
	[%$TOPIC/2fw1se54_en-us_VS_110_1_0_2_0_1%]^ element, 
	[%$TOPIC/2fw1se54_en-us_VS_110_1_0_2_0_2%] inherit
)
static member GetCustomAttributes : 
        element:[%$TOPIC/2fw1se54_en-us_VS_110_1_0_3_0_0%] * 
        inherit:[%$TOPIC/2fw1se54_en-us_VS_110_1_0_3_0_1%] -> [%$TOPIC/2fw1se54_en-us_VS_110_1_0_3_0_2%][] 

Parameters

element
Type: System.ReflectionParameterInfo

An object derived from the ParameterInfo class that describes a parameter of a member of a class.

inherit
Type: SystemBoolean

If true, specifies to also search the ancestors of element for custom attributes.

Return Value

Type: SystemAttribute
An Attribute array that contains the custom attributes applied to element, or an empty array if no such custom attributes exist.
Exceptions
ExceptionCondition
ArgumentException

The Member property of element is null.

ArgumentNullException

element is .

TypeLoadException

A custom attribute type cannot be loaded.

Remarks

If element represents a parameter in a method of a derived type, the return value includes the inheritable custom attributes applied to the same parameter in the overridden base methods.

Examples

The following code example demonstrates the use of GetCustomAttributes, taking a ParameterInfo as a parameter.

Imports System
Imports System.Reflection
Imports System.ComponentModel

Module DemoModule
    Public Class AClass
        ' Add Description and ParamArray (with the keyword) attributes. 
        Public Sub ParamArrayAndDesc( _
            <Description("This argument is a ParamArray")> _
            ByVal ParamArray args() As Integer)
        End Sub 
    End Class 

    Sub Main()
        ' Get the Class type to access its metadata. 
        Dim clsType As Type = GetType(AClass)
        ' Get the type information for the method. 
        Dim mInfo As MethodInfo = clsType.GetMethod("ParamArrayAndDesc")
        ' Get the Parameter information for the method. 
        Dim pInfo() As ParameterInfo = mInfo.GetParameters()
        Dim attr As Attribute
        ' Iterate through each attribute of the parameter. 
        For Each attr In Attribute.GetCustomAttributes(pInfo(0))
            ' Check for the ParamArray attribute. 
            If TypeOf attr Is ParamArrayAttribute Then 
                ' Convert the attribute to access its data. 
                Dim paAttr As ParamArrayAttribute = _
                    CType(attr, ParamArrayAttribute)
                Console.WriteLine("Parameter {0} has the " + _
                    "ParamArray attribute.", pInfo(0).Name)
            ' Check for the Description attribute. 
            ElseIf TypeOf attr Is DescriptionAttribute Then 
                ' Convert the attribute to access its data. 
                Dim descAttr As DescriptionAttribute = _
                    CType(attr, DescriptionAttribute)
                Console.WriteLine("Parameter {0} has a description " + _
                    "attribute. The description is:", pInfo(0).Name)
                Console.WriteLine(descAttr.Description)
            End If 
        Next 
    End Sub 
End Module 

' Output: 
' Parameter args has a description attribute. The description is: 
' This argument is a ParamArray 
' Parameter args has the ParamArray attribute.
using System;
using System.Reflection;
using System.ComponentModel;

namespace CustAttrs5CS {
    public class AClass {
        public void ParamArrayAndDesc(
            // Add ParamArray (with the keyword) and Description attributes.
            [Description("This argument is a ParamArray")]
            params int[] args)
        {}
    }

    class DemoClass {
        static void Main(string[] args) {
            // Get the Class type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for the method.
            MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
            if (mInfo != null) {
                // Get the parameter information.
                ParameterInfo[] pInfo = mInfo.GetParameters();
                if (pInfo != null) {
                    // Iterate through all the attributes for the parameter. 
                    foreach(Attribute attr in 
                        Attribute.GetCustomAttributes(pInfo[0])) {
                        // Check for the ParamArray attribute. 
                        if (attr.GetType() == typeof(ParamArrayAttribute))
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has the ParamArray attribute.",
                                pInfo[0].Name, mInfo.Name);
                        // Check for the Description attribute. 
                        else if (attr.GetType() == 
                            typeof(DescriptionAttribute)) {
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has a description attribute.",
                                pInfo[0].Name, mInfo.Name);
                            Console.WriteLine("The description is: \"{0}\"",
                                ((DescriptionAttribute)attr).Description);
                        }
                    }
                }
            }
        }
    }
}

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 */
using namespace System;
using namespace System::Reflection;
using namespace System::ComponentModel;

namespace CustAttrs5CS
{
   public ref class AClass
   {
   public:
      void ParamArrayAndDesc(
         // Add ParamArray and Description attributes.
         [Description("This argument is a ParamArray")]
         array<Int32>^args )
      {}
   };

   ref class DemoClass
   {
   public:
      static void Main()
      {
         // Get the Class type to access its metadata.
         Type^ clsType = AClass::typeid;

         // Get the type information for the method.
         MethodInfo^ mInfo = clsType->GetMethod( "ParamArrayAndDesc" );
         if ( mInfo != nullptr )
         {
            // Get the parameter information. 
            array<ParameterInfo^>^pInfo = mInfo->GetParameters();
            if ( pInfo != nullptr )
            {
               // Iterate through all the attributes for the parameter.
               System::Collections::IEnumerator^ myEnum4 = Attribute::GetCustomAttributes( pInfo[ 0 ] )->GetEnumerator();
               while ( myEnum4->MoveNext() )
               {
                  Attribute^ attr = safe_cast<Attribute^>(myEnum4->Current);

                  // Check for the ParamArray attribute. 
                  if ( attr->GetType() == ParamArrayAttribute::typeid )
                                    Console::WriteLine( "Parameter {0} for method {1} " 
                  "has the ParamArray attribute.", pInfo[ 0 ]->Name, mInfo->Name );
                  // Check for the Description attribute. 
                  else 

                  // Check for the Description attribute. 
                  if ( attr->GetType() == DescriptionAttribute::typeid )
                  {
                     Console::WriteLine( "Parameter {0} for method {1} " 
                     "has a description attribute.", pInfo[ 0 ]->Name, mInfo->Name );
                     Console::WriteLine( "The description is: \"{0}\"", (dynamic_cast<DescriptionAttribute^>(attr))->Description );
                  }
               }
            }
         }
      }
   };
}

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 */
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.