Write a program for Reflection in C# .Net - Informational Webber

Write a program for Reflection in C# .Net

If you are learning Reflection in C# .Net and faced difficulties while learning this topic, then, you are at the right place. I will provide you the simplest definition and program to learn this important topic. Let us start with the definition, What exactly the reflection is?

In layman terms Reflection means image. Reflection is a namespace in C# language which is applied over assembly to access various information in dll library like name of classes, name of functions, name of namespaces, etc.

For Example, if you go to any hotel or restaurant, they show you the Menu for Breakfast, Lunch or Dinner. Same applies here. But here it applies to assembly.

What is assembly?

Assembly is basically a dll or exe file which we get once we compile the source code using CSC (C# compiler). You must be aware about the compilation and execution phase of C#. We compile the source code with CSC compiler, gets a assembly which then passed onto CLR (Common Language Run-Time known as Virtual Operating system) and JIT (Just-In Time compiler). At last, we get a Machine code according to type of O.S.

Lets come back to our topic Reflection: Why do we require Reflection? There are 3 bullet points below which will definitely help you understand the reason behind reflection.

  • How can we use dll if we do not know the name of classes, functions and namespace used?
  • If we do not know the name of class, how can we perform instantiation?
  • If there is no instantiation, then, how can we call the function?

I hope you understand the reason to use reflection in C# .Net. Let us now understand the concept using a program. Perform following steps to duly understand the concept of reflection in C# .Net

Step 1. Open Developer Command Prompt for Visual Studio

Step 2. Create a directory in C drive on your computer. For Example, type “md given” and hit Enter.

Step 3. Enter into newly created directory using following command: cd given and hit Enter.

Step 4. Open Notepad using VS Command Prompt. Type notepad and hit enter.

Step 5. Create a file with extension .cs  For Example, IT.cs Write a program having a namespace, class and functions without main method.

namespace Shubham
{

public class Employee
{
public string EmpName()
{
return “Munir”;
}
}

}

We will create dll library for this file. Type command in VS CMD > csc /t:library IT.cs  This command will create a dll library for file IT.cs You can verify this by typing dir command in Command prompt.

***Important Note: If a class contains main() then, it will generate .exe file upon compilation. And if a class does not contains main() then it will generate .dll file.

Step 6. Open one more notepad file using Visual Studio cmd and create one more file with extension .cs Let us name that file client.cs This file will contain main method and different static functions which are used to show list of classes, functions, etc. Here is the code with explaination:

using System;
using System.Reflection;
class Test
{
public void Browse()
{
Assembly ptr = Assembly.LoadFile(@”c:\given\IT.dll”); // Load file is a static function used to load dll file.

//Assembly is class used here
Type[] temp = ptr.GetTypes();                          //GetTypes() function helps to display list of classes used in library

//temp is a variable of type Type.
foreach (Type x in temp)
{
Console.WriteLine(x);                                      //ListF() is used to display list of functions
ListF(x);
}

}

public void ListF(Type t)
{
MethodInfo[] m = t.GetMethods();                  //MethodInfo() is a constructor that initializes a new instance of                                                                                        MethodInfo class

//GetMethods() is used to display list of methods used

foreach(MethodInfo p in m)
{
Console.WriteLine(p);
}
}

public static void Main()
{
Test t = new Test();
t.Browse();
}
}

Step 7. Compile the newly created file. For Example, csc client.cs and hit enter

Step 8. Call the exe file and by typing name of program without extension. Solution will be the name of classes, name of functions used (also for object class) and namespaces used.

Hope you like this article. Please do comment and share on this article. Thank you for reading!

2 Replies to “Write a program for Reflection in C# .Net”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.