.NET Framework Class Library
AttributeGetCustomAttribute Method (ParameterInfo, Type, Boolean)

Retrieves a custom attribute 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)
Syntax
Public Shared Function GetCustomAttribute ( _
	element As [%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_0_0_0%], _
	attributeType As [%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_0_0_1%], _
	inherit As [%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_0_0_2%] _
) As [%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_0_0_3%]
public static [%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_1_0_0%] GetCustomAttribute(
	[%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_1_0_1%] element,
	[%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_1_0_2%] attributeType,
	[%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_1_0_3%] inherit
)
public:
static [%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_2_0_0%]^ GetCustomAttribute(
	[%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_2_0_1%]^ element, 
	[%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_2_0_2%]^ attributeType, 
	[%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_2_0_3%] inherit
)
static member GetCustomAttribute : 
        element:[%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_3_0_0%] * 
        attributeType:[%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_3_0_1%] * 
        inherit:[%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_3_0_2%] -> [%$TOPIC/4x2fs7yd_en-us_VS_110_1_0_3_0_3%]

Parameters

element
Type: System.ReflectionParameterInfo

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

attributeType
Type: SystemType

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

inherit
Type: SystemBoolean

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

Return Value

Type: SystemAttribute
A reference to the single custom attribute of type attributeType that is applied to element, or if there is no such attribute.
Exceptions
ExceptionCondition
ArgumentNullException

element or attributeType is .

ArgumentException

attributeType is not derived from Attribute.

AmbiguousMatchException

More than one of the requested attributes was found.

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 defines a custom parameter Attribute class and applies the custom attribute to a method in a derived class and the base of the derived class. The example shows the use of the GetCustomAttribute method to return the attributes.

' Example for the Attribute.GetCustomAttribute( ParameterInfo, Type, Boolean )  
' method. 
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' Define a custom parameter attribute that takes a single message argument.
    <AttributeUsage(AttributeTargets.Parameter)>  _
    Public Class ArgumentUsageAttribute
        Inherits Attribute

        ' This is the attribute constructor. 
        Public Sub New(UsageMsg As String)
            Me.usageMsg = UsageMsg
        End Sub ' New 

        ' usageMsg is storage for the attribute message. 
        Protected usageMsg As String 

        ' This is the Message property for the attribute. 
        Public Property Message() As String 
            Get 
                Return usageMsg
            End Get 
            Set
                usageMsg = value
            End Set 
        End Property 
    End Class ' ArgumentUsageAttribute 

    Public Class BaseClass

        ' Assign an ArgumentUsage attribute to the strArray parameter. 
        ' Assign a ParamArray attribute to the strList parameter. 
        Public Overridable Sub TestMethod( _
            <ArgumentUsage("Must pass an array here.")> _
            strArray() As String, _
            ParamArray strList() As String)
        End Sub ' TestMethod
    End Class ' BaseClass

    Public Class DerivedClass
        Inherits BaseClass

        ' Assign an ArgumentUsage attribute to the strList parameter. 
        ' Assign a ParamArray attribute to the strList parameter. 
        Public Overrides Sub TestMethod( _
            strArray() As String, _
            <ArgumentUsage("Can pass a parameter list or array here.")> _
            ParamArray strList() As String)
        End Sub ' TestMethod
    End Class ' DerivedClass

    Class DemoClass

        Shared Sub DisplayParameterAttributes(mInfo As MethodInfo, _
            pInfoArray() As ParameterInfo, includeInherited As Boolean)

            Console.WriteLine( vbCrLf & _
                "Parameter attribute information for method ""{0}""" & _
                vbCrLf & "includes inheritance from the base class: {1}.", _
                mInfo.Name, IIf(includeInherited, "Yes", "No"))

            ' Display attribute information for the parameters. 
            Dim paramInfo As ParameterInfo
            For Each paramInfo In  pInfoArray

                ' See if the ParamArray attribute is defined. 
                Dim isDef As Boolean = _
                    Attribute.IsDefined(paramInfo, GetType(ParamArrayAttribute))

                If isDef Then
                    Console.WriteLine( vbCrLf & "    The " & _
                        "ParamArray attribute is defined for " & _
                        vbCrLf & "    parameter {0} of method {1}.", _
                        paramInfo.Name, mInfo.Name)
                End If 

                ' See if ParamUsageAttribute is defined.   
                ' If so, display a message. 
                Dim usageAttr As ArgumentUsageAttribute = _
                    Attribute.GetCustomAttribute(paramInfo, _
                        GetType(ArgumentUsageAttribute), _
                        includeInherited)

                If Not (usageAttr Is Nothing) Then
                    Console.WriteLine( vbCrLf & "    The " & _
                        "ArgumentUsage attribute is defined for " & _
                        vbCrLf & "    parameter {0} of method {1}.", _
                        paramInfo.Name, mInfo.Name)
                    Console.WriteLine( vbCrLf & _
                        "        The usage message for {0} is: " & _
                        vbCrLf & "        ""{1}"".", _
                        paramInfo.Name, usageAttr.Message)
                End If 
            Next paramInfo
        End Sub ' DisplayParameterAttributes

        Public Shared Sub Main()
            Console.WriteLine( _
                "This example of Attribute.GetCustomAttribute" & _
                "( ParameterInfo, Type, Boolean )" & vbCrLf & _
                "generates the following output." )

            ' Get the class type, and then get the MethodInfo object  
            ' for TestMethod to access its metadata. 
            Dim clsType As Type = GetType(DerivedClass)
            Dim mInfo As MethodInfo = clsType.GetMethod("TestMethod")

            ' Iterate through the ParameterInfo array for the method parameters. 
            Dim pInfoArray As ParameterInfo() = mInfo.GetParameters()
            If Not (pInfoArray Is Nothing) Then

                DisplayParameterAttributes(mInfo, pInfoArray, False)
                DisplayParameterAttributes(mInfo, pInfoArray, True)
            Else
                Console.WriteLine( _
                    "The parameters information could " & _
                    "not be retrieved for method {0}.", mInfo.Name)
            End If 
        End Sub ' Main

    End Class ' DemoClass
End Namespace ' NDP_UE_VB

' This example of Attribute.GetCustomAttribute( ParameterInfo, Type, Boolean ) 
' generates the following output. 
'  
' Parameter attribute information for method "TestMethod" 
' includes inheritance from the base class: No. 
'  
'     The ParamArray attribute is defined for 
'     parameter strList of method TestMethod. 
'  
'     The ArgumentUsage attribute is defined for 
'     parameter strList of method TestMethod. 
'  
'         The usage message for strList is: 
'         "Can pass a parameter list or array here.". 
'  
' Parameter attribute information for method "TestMethod" 
' includes inheritance from the base class: Yes. 
'  
'     The ArgumentUsage attribute is defined for 
'     parameter strArray of method TestMethod. 
'  
'         The usage message for strArray is: 
'         "Must pass an array here.". 
'  
'     The ParamArray attribute is defined for 
'     parameter strList of method TestMethod. 
'  
'     The ArgumentUsage attribute is defined for 
'     parameter strList of method TestMethod. 
'  
'         The usage message for strList is: 
'         "Can pass a parameter list or array here.".
// Example for the Attribute.GetCustomAttribute( ParameterInfo, Type, Boolean )  
// method. 
using System;
using System.Reflection;

namespace NDP_UE_CS 
{
    // Define a custom parameter attribute that takes a single message argument.
    [AttributeUsage( AttributeTargets.Parameter )]
    public class ArgumentUsageAttribute : Attribute
    {
        // This is the attribute constructor. 
        public ArgumentUsageAttribute( string UsageMsg )
        {
            this.usageMsg = UsageMsg;
        }

        // usageMsg is storage for the attribute message. 
        protected string usageMsg;

        // This is the Message property for the attribute. 
        public string Message
        {
            get { return usageMsg; }
            set { usageMsg = value; }
        }
    }

    public class BaseClass 
    {
        // Assign an ArgumentUsage attribute to the strArray parameter. 
        // Assign a ParamArray attribute to strList using the params keyword. 
        public virtual void TestMethod(
            [ArgumentUsage("Must pass an array here.")]
            String[] strArray,
            params String[] strList)
        { }
    }

    public class DerivedClass : BaseClass
    {
        // Assign an ArgumentUsage attribute to the strList parameter. 
        // Assign a ParamArray attribute to strList using the params keyword. 
        public override void TestMethod(
            String[] strArray,
            [ArgumentUsage("Can pass a parameter list or array here.")]
            params String[] strList)
        { }
    }

    class CustomParamDemo 
    {
        static void Main( ) 
        {
            Console.WriteLine( 
                "This example of Attribute.GetCustomAttribute( Parameter" +
                "Info, Type, Boolean )\ngenerates the following output." );

            // Get the class type, and then get the MethodInfo object  
            // for TestMethod to access its metadata.
            Type clsType = typeof(DerivedClass);
            MethodInfo mInfo = clsType.GetMethod("TestMethod");

            // Iterate through the ParameterInfo array for the method parameters.
            ParameterInfo[] pInfoArray = mInfo.GetParameters();
            if (pInfoArray != null) 
            {
                DisplayParameterAttributes( mInfo, pInfoArray, false );
                DisplayParameterAttributes( mInfo, pInfoArray, true );
            }
            else
                Console.WriteLine("The parameters information could " +
                    "not be retrieved for method {0}.", mInfo.Name);
        }

        static void DisplayParameterAttributes( MethodInfo mInfo,
            ParameterInfo[] pInfoArray, bool includeInherited )
        {
            Console.WriteLine( 
                "\nParameter attribute information for method \"" +
                "{0}\"\nincludes inheritance from base class: {1}.", 
                mInfo.Name, includeInherited ? "Yes" : "No" );

            // Display the attribute information for the parameters. 
            foreach( ParameterInfo paramInfo in pInfoArray )
            {
                // See if the ParamArray attribute is defined. 
                bool isDef = Attribute.IsDefined( paramInfo, 
                    typeof(ParamArrayAttribute));

                if( isDef )
                    Console.WriteLine(
                        "\n    The ParamArray attribute is defined " +
                        "for \n    parameter {0} of method {1}.",
                        paramInfo.Name, mInfo.Name);

                // See if ParamUsageAttribute is defined.   
                // If so, display a message.
                ArgumentUsageAttribute usageAttr = (ArgumentUsageAttribute)
                    Attribute.GetCustomAttribute( paramInfo, 
                        typeof(ArgumentUsageAttribute),
                        includeInherited );

                if( usageAttr != null )
                {
                    Console.WriteLine( 
                        "\n    The ArgumentUsage attribute is def" +
                        "ined for \n    parameter {0} of method {1}.",
                        paramInfo.Name, mInfo.Name );

                    Console.WriteLine( "\n        The usage " +
                        "message for {0} is:\n        \"{1}\".",
                        paramInfo.Name, usageAttr.Message);
                }
            }
        }
    }
}

/*
This example of Attribute.GetCustomAttribute( ParameterInfo, Type, Boolean )
generates the following output.

Parameter attribute information for method "TestMethod"
includes inheritance from base class: No.

    The ParamArray attribute is defined for
    parameter strList of method TestMethod.

    The ArgumentUsage attribute is defined for
    parameter strList of method TestMethod.

        The usage message for strList is:
        "Can pass a parameter list or array here.".

Parameter attribute information for method "TestMethod"
includes inheritance from base class: Yes.

    The ArgumentUsage attribute is defined for
    parameter strArray of method TestMethod.

        The usage message for strArray is:
        "Must pass an array here.".

    The ParamArray attribute is defined for
    parameter strList of method TestMethod.

    The ArgumentUsage attribute is defined for
    parameter strList of method TestMethod.

        The usage message for strList is:
        "Can pass a parameter list or array here.".
*/
// Example for the Attribute::GetCustomAttribute( ParameterInfo*, Type*, bool )  
// method. 
using namespace System;
using namespace System::Collections;
using namespace System::Reflection;

namespace NDP_UE_CPP
{
   // Define a custom parameter attribute that takes a single message argument.

   [AttributeUsage(AttributeTargets::Parameter)]
   public ref class ArgumentUsageAttribute: public Attribute
   {
   protected:

      // usageMsg is storage for the attribute message.
      String^ usageMsg;

   public:

      // This is the attribute constructor.
      ArgumentUsageAttribute( String^ UsageMsg )
      {
         this->usageMsg = UsageMsg;
      }

      property String^ Message 
      {
         // This is the Message property for the attribute.
         String^ get()
         {
            return usageMsg;
         }

         void set( String^ value )
         {
            this->usageMsg = value;
         }
      }
   };

   public ref class BaseClass
   {
   public:

      // Assign an ArgumentUsage attribute to the strArray parameter. 
      // Assign a ParamArray attribute to strList. 
      virtual void TestMethod( [ArgumentUsage("Must pass an array here.")]array<String^>^strArray,
                               ...array<String^>^strList ){}
   };

   public ref class DerivedClass: public BaseClass
   {
   public:

      // Assign an ArgumentUsage attributes to the strList parameter. 
      virtual void TestMethod( array<String^>^strArray, [ArgumentUsage(
      "Can pass a parameter list or array here.")]array<String^>^strList ) override {}

   };

   void DisplayParameterAttributes( MethodInfo^ mInfo, array<ParameterInfo^>^pInfoArray, bool includeInherited )
   {
      Console::WriteLine( "\nParameter attribute information for method \"{0}"
      "\"\nincludes inheritance from base class: {1}.", mInfo->Name, includeInherited ? (String^)"Yes" : "No" );

      // This implements foreach( ParameterInfo* paramInfo in pInfoArray ).
      IEnumerator^ myEnum = pInfoArray->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         ParameterInfo^ paramInfo = safe_cast<ParameterInfo^>(myEnum->Current);

         // See if the ParamArray attribute is defined. 
         bool isDef = Attribute::IsDefined( paramInfo, ParamArrayAttribute::typeid );
         if ( isDef )
                  Console::WriteLine( "\n    The ParamArray attribute is defined " 
         "for \n    parameter {0} of method {1}.", paramInfo->Name, mInfo->Name );

         // See if ParamUsageAttribute is defined.   
         // If so, display a message.
         ArgumentUsageAttribute^ usageAttr = static_cast<ArgumentUsageAttribute^>(Attribute::GetCustomAttribute( paramInfo, ArgumentUsageAttribute::typeid, includeInherited ));
         if ( usageAttr != nullptr )
         {
            Console::WriteLine( "\n    The ArgumentUsage attribute is defined " 
            "for \n    parameter {0} of method {1}.", paramInfo->Name, mInfo->Name );
            Console::WriteLine( "\n        The usage " 
            "message for {0} is:\n        \"{1}\".", paramInfo->Name, usageAttr->Message );
         }
      }
   }

}

int main()
{
   Console::WriteLine( "This example of Attribute::GetCustomAttribute( ParameterInfo*, " 
   "Type*, bool )\ngenerates the following output." );

   // Get the class type, and then get the MethodInfo object  
   // for TestMethod to access its metadata.
   Type^ clsType = NDP_UE_CPP::DerivedClass::typeid;
   MethodInfo^ mInfo = clsType->GetMethod(  "TestMethod" );

   // Iterate through the ParameterInfo array for the method parameters. 
   array<ParameterInfo^>^pInfoArray = mInfo->GetParameters();
   if ( pInfoArray != nullptr )
   {
      NDP_UE_CPP::DisplayParameterAttributes( mInfo, pInfoArray, false );
      NDP_UE_CPP::DisplayParameterAttributes( mInfo, pInfoArray, true );
   }
   else
      Console::WriteLine( "The parameters information could " 
   "not be retrieved for method {0}.", mInfo->Name );
}

/*
This example of Attribute::GetCustomAttribute( ParameterInfo*, Type*, bool )
generates the following output.

Parameter attribute information for method "TestMethod"
includes inheritance from base class: No.

    The ParamArray attribute is defined for
    parameter strList of method TestMethod.

    The ArgumentUsage attribute is defined for
    parameter strList of method TestMethod.

        The usage message for strList is:
        "Can pass a parameter list or array here.".

Parameter attribute information for method "TestMethod"
includes inheritance from base class: Yes.

    The ArgumentUsage attribute is defined for
    parameter strArray of method TestMethod.

        The usage message for strArray is:
        "Must pass an array here.".

    The ParamArray attribute is defined for
    parameter strList of method TestMethod.

    The ArgumentUsage attribute is defined for
    parameter strList of method TestMethod.

        The usage message for strList is:
        "Can pass a parameter list or array here.".
*/
Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
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.