Mega Code Archive

 
Categories / VB.Net / Reflection
 

Get the nonpublic properties

Imports System Imports System.Reflection Imports System.Reflection.Emit Imports Microsoft.VisualBasic Public Class MyTypeClass     Public ReadOnly Property MyProperty1() As [String]         Get             Return "hello"         End Get     End Property     Public ReadOnly Property MyProperty2() As [String]         Get             Return "hello"         End Get     End Property     Protected ReadOnly Property MyProperty3() As [String]         Get             Return "hello"         End Get     End Property End Class Public Class TypeMain     Public Shared Sub Main()         Dim myType As Type = GetType(MyTypeClass)         Dim myPropertyInfo1 As PropertyInfo() = myType.GetProperties((BindingFlags.NonPublic Or BindingFlags.Instance))         Console.WriteLine("The number of protected properties is {0}.", myPropertyInfo1.Length.ToString())         DisplayPropertyInfo(myPropertyInfo1)     End Sub      Public Shared Sub DisplayPropertyInfo(ByVal myPropertyInfo() As PropertyInfo)         Dim i As Integer         For i = 0 To myPropertyInfo.Length - 1             Dim myPropInfo As PropertyInfo = CType(myPropertyInfo(i), PropertyInfo)             Console.WriteLine("The property name is {0}.", myPropInfo.Name.ToString())             Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType.ToString())         Next i     End Sub  End Class