Resources » .NET programming » .NET Framework

Reflection in .NET


Posted Date: 11-Mar-2004  Last Updated:   Category: .NET Framework    
Author: Member Level: Gold    Points: 7


The article presents information about Reflection. This is useful to a beginner for concept of Reflection



Introduction:
The article introduces the reader to the concept of Reflection in DotNet. However, the article will speak only a little part of the big concept of Reflection. By the end of this article, the user can get an introductory hold of what reflection is all about and a simple sample for clear explanation. Some of the definitions/explanations are given to narrow down only to the concepts of Reflection. Also the sample provided is written in Visual C# .NET.

Why Reflection?
In the old COM days, we wrote all the business logic in a common DLL (and called it a component) for many reasons like extensibility, scalability and all the available “??bility”. Now if somebody has written the DLL, and you want to use it, then how would you know the methods and types inside the component? We cannot find this at runtime (meaning programmatically we cannot retrieve the internal information of the DLL); the best possible thing that can be done is, we can open the DLL with the Visual Studio tool – OLE/COM Object viewer to know the internal details, but still getting a clear picture of the component would be very tough. Another easier way is to go to Visual Basic Editor and add the component as reference, followed by use the Object Editor to know about the details of the DLL.

So the mechanism of retrieving the internal details of a DotNET component (check out the word DotNet before the word component) in runtime is done with the help of Reflection in .NET. Strictly speaking the word “component” should be replaced with the word “Assembly”, when we speak in DotNet Context, but still I would prefer it to call as component for making things a bit easy.

Going through the proper direction:

It is always nice to start with an example which would help for easier understanding. So with the above description of Why Reflection is, we would write a DotNet Reflection program step by step to feel better. The idea is to first write a DotNet Component (a DLL) and secondly write a reflection program to retrieve the internal details of the component being written.

Creating a Component:

Steps to be followed:

1. Create a new Class Library solution.
2. Let the Name be “SampleComponent”.
3. As usual VS .NET gives you by default, a Class1.cs and AssemblyInfo.cs file.
4. We would add a method called “Add2Nums” in Class1.cs file, which does the job of adding any two numbers. So the Class1.cs file looks like

namespace SampleComponent
{
///
/// Summary description for Class1.
///
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public int Add2Nums(int FirstNum, int SecondNum)
{
return (FirstNum + SecondNum );
}
}
}


Writing the Reflection Program:
Steps to be followed:
1. Create a new Windows Application
2. Design the Form
a. Add a new textbox named “txtAssembly” to give the path of the component (DLL).
b. Add a Command Button named “cmdGetInfo”, used to get the information of the component.
3. Add a Namespace at the top of the section

using System.Reflection;


And the Actual code goes like this in the Command Button


private void cmdGetInfo_Click(object sender, System.EventArgs e)
{
Assembly SampleAssembly; //Assembly Variable
Type[] ArrType; //Assembly Type Collection

//Load the Assembly
SampleAssembly = Assembly.LoadFrom(@txtAssembly.Text);

// Get All the Types in the Assembly
ArrType = SampleAssembly.GetTypes();

//Loop through the Types inside the Assembly,
// in our case, only one type - Class
foreach (Type Cls in ArrType)
{

// Get All the Methods of a Particular Class, It
// can be a Public/Instant/To Avoid Members declared at
// the hierarchy level -(To Avoid methods like GetHashCode, Equals))

MethodInfo[] Meths = Cls.GetMethods (BindingFlags.Public
|BindingFlags.Instance|BindingFlags.DeclaredOnly );
foreach (MethodInfo Method in Meths)
{
//List out the Method Names
MessageBox.Show ( Method.Name ) ;
}
}


So the above sample would have helped us to know the methods inside the Assembly.

I hope the above article and sample would have provided a overview of Reflection and its basics.


Did you like this resource? Share it with your friends and show your love!

Responses to "Reflection in .NET"
Author: Santosh Jadhav    06 May 2004Member Level: Bronze   Points : 0
The overall article is good, but too short :). Got to know exactly what Reflection are. Would like to have probabaly in next article the indepth of Reflection.

Thanks



Author: Nandakumar    17 Sep 2004Member Level: Bronze   Points : 0
U tried to tell something about reflection but its not enough.
This is my personal opinion ,bcos i have some idea about .net
& dll eventhough i couldnt undestand ur point,so how can fresher understand ur point.So plz explain clearly



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    kelly456
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India