Reflection in VB.NET
This article shows an introduction of reflection in VB.NET.
In this article we will learn how to use reflection in VB.NET.
Reflection: Reflection is the capability to get information from assemblies about the contained types, their members, their accessibility, attributes and so more. You can programmatically access the properties of any assembly or the currently executing assembly or any type using reflection.
-
A method to dynamically know about the class, structure, interfaces etc.
-
We can easily view all the fields, methods, constructors etc.
-
All related classes are provided under System.Reflection namespace.Use Type class to get information about the class and other itemTo get reference of a class or structure etc.
-
use any of two methods
- Using getType() method from an object.
- Using typeof() operator with class, structure etc.
For example:
Imports System.Reflection
Module module1
Class Program
Public Shared Sub Main(ByVal args As String())
'Dim t As Type = typeof(Math )
Dim s As String = "amit"
Dim t As Type = s.[GetType]()
Dim f As FieldInfo() = t.GetFields()
Dim m As MethodInfo() = t.GetMethods()
Console.WriteLine("Total Fields are {0}", f.Length)
For i As Integer = 0 To f.Length - 1
Console.WriteLine("-->{0} : {1}", i + 1, f(i))
Next
Console.WriteLine("Total Methods are {0}", m.Length)
For i As Integer = 0 To m.Length - 1
Console.WriteLine("-->{0} : {1}", i + 1, m(i))
Next
End Sub
End Class
End Module
Output: