Attribute.GetCustomAttribute Method (ParameterInfo, Type)
Retrieves a custom attribute applied to a method parameter. Parameters specify the method parameter, and the type of the custom attribute to search for.
Assembly: mscorlib (in mscorlib.dll)
Public Shared Function GetCustomAttribute ( element As ParameterInfo, attributeType As Type ) As Attribute
Parameters
- element
-
Type:
System.Reflection.ParameterInfo
An object derived from the ParameterInfo class that describes a parameter of a member of a class.
- attributeType
-
Type:
System.Type
The type, or a base type, of the custom attribute to search for.
Return Value
Type: System.AttributeA reference to the single custom attribute of type attributeType that is applied to element, or null if there is no such attribute.
| Exception | Condition |
|---|---|
| ArgumentNullException | element or attributeType is null. |
| 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. |
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 ) 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 strList using the ParamArray keyword. 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 strList using the ParamArray keyword. 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 Module CustomParamDemo Sub Main() Console.WriteLine( _ "This example of Attribute.GetCustomAttribute" & _ "( ParameterInfo, Type )" & 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 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)) 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 Else Console.WriteLine( _ "The parameters information could " & _ "not be retrieved for method {0}.", mInfo.Name) End If End Sub ' Main End Module ' DemoClass End Namespace ' NDP_UE_VB ' This example of Attribute.GetCustomAttribute( ParameterInfo, Type ) ' generates the following output. ' ' 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.".
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0