ARTICLE

Runtime Type Information (Reflection) in VB.NET

Posted by Manish Tewatia Articles | Visual Basic Language August 13, 2010
In this article I will explain you about Runtime Type Information (Reflection) in VB.NET.
Download Files:
 
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor