Attribute::GetCustomAttributes Method (ParameterInfo^, Type^, Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

static array<Attribute^>^ GetCustomAttributes(
	ParameterInfo^ element,
	Type^ attributeType,
	bool inherit


Type: System.Reflection::ParameterInfo^

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

Type: System::Type^

The type, or a base type, of the custom attribute to search for.

Type: System::Boolean

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

Return Value

Type: array<System::Attribute^>^

An Attribute array that contains the custom attributes of type attributeType applied to element, or an empty array if no such custom attributes exist.

Exception Condition

element or attributeType is null.


attributeType is not derived from Attribute.


A custom attribute type cannot be loaded.

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.

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

using namespace System;
using namespace System::Reflection;
using namespace System::ComponentModel;

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

   ref class DemoClass
      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.

                  // 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.

.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top