Visual Basic Reflection

In visual basic, Reflection is useful to get the type information that describes assemblies, modules, members, parameters and other entities in the managed code by examining their metadata.

 

In visual basic, the System.Reflection namespace will contain all the classes that provide access to the metadata of managed code to get the type information.

 

We can also use reflection to create type instances dynamically at runtime, bind the type to an existing object or get the type from an existing object and invoke its methods or access its fields and properties. In case, if we define any attributes in our code, those also can be accessed by using reflection.

Visual Basic Reflection Example

Following is the example of using reflection to get the class object properties and values in visual basic.

 

Imports System.Reflection

 

Module Module1

Sub Main(ByVal args As String())

Dim items As List(Of userdetails) = New List(Of userdetails)()

items.Add(New userdetails With {.userid = 1, .username = "suresh", .location = "chennai"})

items.Add(New userdetails With {.userid = 2, .username = "rohini", .location = "guntur"})

items.Add(New userdetails With {.userid = 3, .username = "praveen", .location = "bangalore"})

items.Add(New userdetails With {.userid = 4, .username = "sateesh", .location = "vizag"})

items.Add(New userdetails With {.userid = 5, .username = "madhav", .location = "nagpur"})

items.Add(New userdetails With {.userid = 6, .username = "honey", .location = "nagpur"})

Dim strmsg As String = String.Empty

For Each user In items

strmsg = GetPropertyValues(user)

Console.WriteLine(strmsg)

Next

Console.ReadLine()

End Sub

Private Function GetPropertyValues(ByVal user As userdetails) As String

Dim type As Type = user.[GetType]()

Dim props As PropertyInfo() = type.GetProperties()

Dim str As String = "{"

For Each prop In props

str += (prop.Name & ":" & prop.GetValue(user) & ":" & prop.PropertyType.Name) & ","

Next

Return str.Remove(str.Length - 1) & "}"

End Function

Class userdetails

Public Property userid As Integer

Public Property username As String

Public Property location As String

End Class

End Module

If you observe the above example, we added System.Reflection namespace to get the userdetails class object property details. Here, we used a Type & PropertyInfo classes to get the required object property details.

 

When we execute the above program, we will get the result like as shown below.

 

Visual Basic Reflection Example Result

 

If you observe the above result, we are able to get the userdetails class object property details including name, value, and type.

Visual Basic Reflection Overview

The following are the important points which we need to remember about reflection in visual basic.

 

  • Reflection is useful to get the type information that describes assemblies, modules, members, parameters and other entities in the managed code by examining their metadata.
  • By reflection, we can create type instances dynamically at runtime, bind or get the type from an existing object and invoke its methods or access its fields and properties.
  • It provides access to perform late binding and get methods type information created at runtime.