Mega Code Archive

 
Categories / VB.Net Tutorial / Reflection
 

GetCustomAttributes taking a ParameterInfo as a parameter

Imports System Imports System.Reflection Imports System.ComponentModel     Public Class AClass         Public Sub ParamArrayAndDesc(<Description("This argument is a ParamArray")> _             ByVal ParamArray args() As Integer)         End Sub     End Class Module DemoModule     Sub Main()         Dim clsType As Type = GetType(AClass)         Dim mInfo As MethodInfo = clsType.GetMethod("ParamArrayAndDesc")         Dim pInfo() As ParameterInfo = mInfo.GetParameters()         Dim attr As Attribute         For Each attr In Attribute.GetCustomAttributes(pInfo(0))             If TypeOf attr Is ParamArrayAttribute Then                 Dim paAttr As ParamArrayAttribute = CType(attr, ParamArrayAttribute)                 Console.WriteLine("Parameter {0} has the ParamArray attribute.", pInfo(0).Name)             ElseIf TypeOf attr Is DescriptionAttribute Then                 Dim descAttr As DescriptionAttribute = CType(attr, DescriptionAttribute)                 Console.WriteLine("Parameter {0} has a description attribute. The description is:", pInfo(0).Name)                 Console.WriteLine(descAttr.Description)             End If         Next     End Sub End Module