Export (0) Print
Expand All

Attribute.GetCustomAttribute 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)

public static Attribute GetCustomAttribute (
	ParameterInfo element,
	Type attributeType,
	bool inherit
)
public static Attribute GetCustomAttribute (
	ParameterInfo element, 
	Type attributeType, 
	boolean inherit
)
public static function GetCustomAttribute (
	element : ParameterInfo, 
	attributeType : Type, 
	inherit : boolean
) : Attribute

Parameters

element

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

attributeType

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

inherit

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

Return Value

A reference to the single custom attribute of type attributeType that is applied to element, or a null reference (Nothing in Visual Basic) if there is no such attribute.

Exception typeCondition

ArgumentNullException

element or attributeType is a null reference (Nothing in Visual Basic).

ArgumentException

attributeType is not derived from Attribute.

AmbiguousMatchException

More than one of the requested attributes was found.

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 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.
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, Boolean )
// method.
package NDP_UE_JSL; 

import System.*;
import System.Reflection.*;
   
// Define a custom parameter attribute that takes a single message argument.
/** @attribute AttributeUsage(AttributeTargets.Parameter)
 */
public class ArgumentUsageAttribute extends Attribute
{
    // This is the attribute constructor.
    public ArgumentUsageAttribute(String usgMsg)
    {
        this.usageMsg = usgMsg;
    } //ArgumentUsageAttribute

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

    // This is the Message property for the attribute.
    /** @property 
     */
    public String get_Message()
    {
        return usageMsg;
    }//get_Message

    /** @property 
     */
    public void set_Message(String value)
    {
        usageMsg = value;
    }//set_Message
} //ArgumentUsageAttribute  
   
public class BaseClass
{
    // Assign an ArgumentUsage attribute to the strArray parameter.
    // Assign a ParamArray attribute to strList using the params keyword.
    public void TestMethod(
        /** @attribute ArgumentUsage("Must pass an array here.")
         */
        String strArray[], 
        /** @attribute System.ParamArray() 
         */
        String strList[])
    {
    } //TestMethod
} //BaseClass
      
public class DerivedClass extends BaseClass
{
    // Assign an ArgumentUsage attribute to the strList parameter.
    // Assign a ParamArray attribute to strList using the params keyword.
    public void TestMethod(String strArray[],
        /** @attribute ArgumentUsage("Can pass a parameter list or array here.")
        */
        /** @attribute System.ParamArray() 
        */
        String strList[])
    {
    } //TestMethod
} //DerivedClass
   
class CustomParamDemo
{
    public static void main(String[] args)
    {
        Console.WriteLine(("This example of Attribute.GetCustomAttribute( "
            + "ParameterInfo, Type, Boolean )\n"
            + "generates the following output."));

        // Get the class type, and then get the MethodInfo object 
        // for TestMethod to access its metadata.
        Type clsType = DerivedClass.class.ToType();
        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.get_Name());
        }
    } //main

    static void DisplayParameterAttributes(MethodInfo mInfo,
        ParameterInfo pInfoArray[], boolean includeInherited)
    {
        Console.WriteLine("\nParameter attribute information for method \"" 
            + "{0}\"\nincludes inheritance from base class: {1}.",
            mInfo.get_Name(), (includeInherited) ? "Yes" : "No");
        for (int iCtr = 0; iCtr < pInfoArray.length; iCtr++) {
            // Display the attribute information for the parameters.
            ParameterInfo paramInfo = pInfoArray[iCtr];
            // See if the ParamArray attribute is defined.
            boolean isDef = Attribute.IsDefined(paramInfo, 
                ParamArrayAttribute.class.ToType());
            if (isDef) {
                Console.WriteLine("\n    The ParamArray attribute is defined " 
                    + "for \n    parameter {0} of method {1}.", 
                    paramInfo.get_Name(), mInfo.get_Name());
            }

            // See if ParamUsageAttribute is defined.  
            // If so, display a message.
            ArgumentUsageAttribute usageAttr = ((ArgumentUsageAttribute)
                (Attribute.GetCustomAttribute(paramInfo, 
                ArgumentUsageAttribute.class.ToType(), includeInherited)));
            if (usageAttr != null) {
                Console.WriteLine("\n    The ArgumentUsage attribute is " 
                    + "defined for \n    parameter {0} of method {1}.", 
                    paramInfo.get_Name(), mInfo.get_Name());
                Console.WriteLine("\n        The usage message for " 
                    + "{0} is:\n        \"{1}\".", paramInfo.get_Name(),
                    usageAttr.get_Message());
            }
        }
    } //DisplayParameterAttributes
} //CustomParamDemo
   
/*
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.".
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft