Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working with Reflection in VB.NET

Author: Aadil
Download Source Code : 230_ReflectionTest.zip

This article will show you what reflection is and how it can be helpful in developing application using .NET

Reflection is a very nice feature introduced in the Microsoft .NET Framework. When you compile your program code for the .NET Framework to execute it is compiled into the MSIL (Microsoft Intermediate Language) codes and this code is placed in files called assemblies. Unlike DLL’s, the previous containers for compiled codes, assemblies are self explaining. This means they can describe their content unlike COM DLL’s which need a type library and need to store their information in the system’s registry.

 

Now 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.

 

The Reflection related classes are contained in a namespace called System.Reflection and the main class that is used is Type. To load an external assembly you need to get the reference to that assembly using Assembly class’s Load or LoadFrom method. Then to get all the types present, you can call the GetType or GetTypes method.

 

In the sample application that I am attaching with this article I’ve created a test class with some public and private members and used reflection on this to get information about this class. I get the reference to the Type object of this class using the GetType method which returns a reference to the Type of the class. Then I’ve enumerated on the Methods present in this class and displayed them in a ListView control.

 

You can change and experiment with the code to see how it works. I’ve reflected the information about the Methods of this type you can also get information about the Type itself, weather it is public, is this class sealed and so. Try to go through the .NET Framework SDK and you’ll see a lot of useful members of the Type class.

Article Comments
I want to be able access a Collection in the main application from my user controls to write a new value into the collection.

Posted on 4/12/2007 11:48:28 AM by Todd

Thanks Aadil,
Great Work.
Please explain about, how to get the Getvalues() with the Method info that retrieves the Values for Methods.

For eg. if the class object have a method Getname(index as integer) How can I get the values returned by that Function

Thanks

Posted on 10/20/2010 7:02:23 AM by Santhosh

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Using a TabControl in Visual Basic.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net