ParameterInfo.GetCustomAttributes gets all the custom attributes defined on this parameter. : ParameterInfo « Reflection « VB.Net






ParameterInfo.GetCustomAttributes gets all the custom attributes defined on this parameter.

 

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

<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
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()
        Dim myType As Type = GetType(MyClass1)
        Dim myMethods As MethodInfo() = myType.GetMethods()
        For i As Integer = 0 To myMethods.Length - 1
            Dim myParameters As ParameterInfo() = myMethods(i).GetParameters()
            If myParameters.Length > 0 Then
                Console.WriteLine(myMethods(i))
                For j As Integer = 0 To myParameters.Length - 1
                    Dim myAttributes As Object() = myParameters(j).GetCustomAttributes(GetType(MyAttribute), False)
                    If myAttributes.Length > 0 Then
                        Console.WriteLine(myParameters(j).Position)
                        Console.WriteLine(myParameters(j).Name)
                        Console.WriteLine(myParameters(j).ParameterType)
                        For k As Integer = 0 To myAttributes.Length - 1
                            Console.WriteLine("{0}", myAttributes(k))
                        Next k
                    End If
                Next j
            End If
        Next i
    End Sub
End Class

   
  








Related examples in the same category

1.ParameterInfo.Attributes gets the attributes for this parameter.
2.ParameterInfo.IsDefined
3.ParameterInfo.IsOut Property tells whether this is an output parameter.
4.ParameterInfo.Name Property returns the name of the parameter.
5.ParameterInfo.ParameterType Property gets the Type of this parameter.