本文整理汇总了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 :