ParameterInfo::GetCustomAttributes Method (Boolean)
Gets all the custom attributes defined on this parameter.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- inherit
-
Type:
System::Boolean
This argument 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.
| Exception | Condition |
|---|---|
| TypeLoadException | A custom attribute type could not be loaded. |
This method ignores the inherit parameter. To search the inheritance chain for attributes on parameters, use the appropriate overloads of the Attribute::GetCustomAttributes method.
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.
using namespace System; using namespace System::Reflection; // Define a custom attribute with one named parameter. [AttributeUsage(AttributeTargets::Parameter)] public ref class MyAttribute: public Attribute { private: String^ myName; public: MyAttribute( String^ name ) { myName = name; } property String^ Name { String^ get() { return myName; } } }; // Define a class which has a custom attribute associated with one of the // parameters of a method. public ref class MyClass1 { public: void MyMethod( [MyAttribute("This is an example parameter attribute")] int i ) {} }; void main() { // Get the type of the class 'MyClass1'. Type^ myType = MyClass1::typeid; // Get the members associated with the class 'MyClass1'. array<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. array<ParameterInfo^>^myParameters = myMethods[ i ]->GetParameters(); if ( myParameters->Length > 0 ) { Console::WriteLine( "\nThe parameters for the method \"{0}\" that have custom attributes are:", myMethods[ i ] ); for ( int j = 0; j < myParameters->Length; j++ ) { // Get the attributes of type 'MyAttribute' for each parameter. array<Object^>^myAttributes = myParameters[ j ]->GetCustomAttributes( MyAttribute::typeid, false ); if ( myAttributes->Length > 0 ) { Console::WriteLine( "Parameter {0}, name = {1}, type = {2} has attributes:", myParameters[ j ]->Position, myParameters[ j ]->Name, myParameters[ j ]->ParameterType ); for ( int k = 0; k < myAttributes->Length; k++ ) { Console::WriteLine( "\t{0}", myAttributes[ k ] ); } } } } } } /* 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 : */
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0