本文整理匯總了VB.NET中System.Reflection.ParameterInfo.GetCustomAttributes方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET ParameterInfo.GetCustomAttributes方法的具體用法?VB.NET ParameterInfo.GetCustomAttributes怎麽用?VB.NET ParameterInfo.GetCustomAttributes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Reflection.ParameterInfo
的用法示例。
在下文中一共展示了ParameterInfo.GetCustomAttributes方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: MyClass1
' 導入命名空間
Imports System.Reflection
' Define a custom attribute with one named parameter.
<AttributeUsage(AttributeTargets.Parameter)> Public Class MyAttribute
Inherits Attribute
Private myName As String
Public Sub New(ByVal name As String)
myName = name
End Sub
Public ReadOnly Property Name() As String
Get
Return myName
End Get
End Property
End Class
' Define a class which has a custom attribute associated with one of
' the parameters of a method.
Public Class MyClass1
Public Sub MyMethod( _
<MyAttribute("This is an example parameter attribute")> _
ByVal i As Integer _
)
Return
End Sub
End Class
Public Class MemberInfo_GetCustomAttributes
Public Shared Sub Main()
' Get the type of the class 'MyClass1'.
Dim myType As Type = GetType(MyClass1)
' Get the members associated with the class 'MyClass1'.
Dim myMethods As MethodInfo() = myType.GetMethods()
' Display the attributes for each of the parameters of each method of the class 'MyClass1'.
For i As Integer = 0 To myMethods.Length - 1
' Get the parameters for the method.
Dim myParameters As ParameterInfo() = myMethods(i).GetParameters()
If myParameters.Length > 0 Then
Console.WriteLine(vbCrLf & "The parameters for the method {0} that have custom attributes are : ", myMethods(i))
For j As Integer = 0 To myParameters.Length - 1
' Get the attributes of type 'MyAttribute' for each parameter.
Dim myAttributes As Object() = myParameters(j).GetCustomAttributes(GetType(MyAttribute), False)
If myAttributes.Length > 0 Then
Console.WriteLine("Parameter {0}, name = {1}, type = {2} has attributes: ", _
myParameters(j).Position, myParameters(j).Name, myParameters(j).ParameterType)
For k As Integer = 0 To myAttributes.Length - 1
Console.WriteLine(vbTab & "{0}", myAttributes(k))
Next k
End If
Next j
End If
Next i
End Sub
End Class
輸出:
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 :