Runtime Type Information (Reflection) in VB.NET

In this article I will explain you about Runtime Type Information (Reflection) in VB.NET.
  • 3002
 

Reflection is the sole mechanism of discovering class information at runtime. You can create instances of classes on the fly at runtime using Reflection namespace members.

The ability to list a type's members is a great way to quickly discover which elements are available. It is an important tool for reporting on your system as well as assisting in the development of user documentation. Using the Reflection namespace, you can control what kind of members you want to show to your users, as well as other information (such as the visibility of a particular method). You can also get information on all of the members in a class or specify only certain subsets (such as methods or fields). Also you can use this neat feature when documenting class hierarchy.

Example of Reflection

    ' example object creation factory
    Imports System.Reflection
    ' simple class
    Class [MyClass]
        Public Shared x As Integer = 0
            Public Sub prompt(ByVal param1 As String)
                Console.WriteLine("MyClass… " & param1)
            End Sub

            Public
Shared Sub prompt2()
                Console.WriteLine("Here is a message from prompt2.")
            End Sub
    End
Class

    Class
TheApp
        Shared Sub Main()
            ' create the Type object
            Dim type1 As Type = GetType([MyClass])

            'or
            ' Type type1 = Type.GetType("MyClass");
            ' create an instance of that type
            Dim o As [Object] = Activator.CreateInstance(type1)
            DirectCast(o, [MyClass]).prompt("hello1")

            ' declare and populate the arrays to hold the information...
            Dim fi As FieldInfo() = type1.GetFields(BindingFlags.[Default] Or BindingFlags.[Static] Or BindingFlags.NonPublic Or BindingFlags.[Public])
            ' fields
            Dim mi As MethodInfo() = type1.GetMethods(BindingFlags.[Default] Or BindingFlags.[Static] Or BindingFlags.NonPublic Or BindingFlags.[Public])

            ' methods
            ' iterate through all the method members
            For Each m As MethodInfo In mi
                Console.WriteLine(m)
            Next

            ' iterate through all the field members
            For Each f As FieldInfo In fi
                Console.WriteLine(f)
            Next
            Dim
argValues As Object() = New Object() {"Hello2"}
            Dim argNames As [String]() = New [String]() {"param1"}

            ' call the requested method with filled parameters
            type1.InvokeMember("prompt", BindingFlags.[Default] Or BindingFlags.InvokeMethod, Nothing, o, argValues, Nothing, _
            Nothing, argNames)
            Console.ReadLine()
        End Sub
    End
Class

Screen Output Generated from the above example is:

ref1.gif

The reflection mechanism is also useful when developing applications that use third-party COM+ components at your site or in the market because you can use Reflection to accomplish late binding to COM objects via the IDispatch interface.

Conclusion

Hope this article would have helped you in understanding Runtime Type Information (Reflection) in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.