ParameterInfo.IsDefined : ParameterInfo « Reflection « VB.Net






ParameterInfo.IsDefined

 

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

<AttributeUsage(AttributeTargets.Parameter)> _
Public Class MyDerivedAttribute
    Inherits MyAttribute

    Public Sub New(ByVal name As String)
        MyBase.New(name)
    End Sub
End Class

Public Class MyClass1

    Public Sub MyMethod(<MyAttribute("This is an example parameter attribute")> _
                        ByVal i As Integer, _
                        <MyDerivedAttribute("This is another parameter attribute")> _
                        ByVal j As Integer, _
                        ByVal k 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 Each mi As MethodInfo In myMethods
            Dim myParameters As ParameterInfo() = mi.GetParameters()
            If myParameters.Length > 0 Then
                For Each pi As ParameterInfo In myParameters
                    If pi.IsDefined(GetType(MyAttribute), False) Then
                        Console.WriteLine(pi.Position)
                        Console.WriteLine(pi.Name)
                        Console.WriteLine(pi.ParameterType)
                    End If
                Next
            End If
        Next
    End Sub
End Class

   
  








Related examples in the same category

1.ParameterInfo.Attributes gets the attributes for this parameter.
2.ParameterInfo.GetCustomAttributes gets all the custom attributes defined on this parameter.
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.