When To Use Reflection In C#

Introduction

In general, Reflection is change the direction of an image in another medium.

In C#, Reflection is a process by which we can get the detailed information from assembly, like about method, constructor, attribute, property, module, global class, parameters, and many more at run time. Reflection can modify structure and behavior of a computer program.
 

What is Reflection in C#

"Reflection is a mechanism by which we can get metadata information from assembly at run time."

When use Reflection

If we want to know assembly information at run time ,then we use reflection. Reflection are used for data binding in .NET Framework. It is also used for testing in .NET Framework.

How to use

There are System.Reflection is the namespace for the reflection. System.Reflection namespace defines the assembly module, MemberInfo, PropertyInfo, MethodInfo, ConstructorInfo, FieldInfo, EventInfo etc.

The System.Type is the base class for reflection. System.Type find the type name, namespace, module type etc. System.Type is also an abstract class.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Reflection;  
  10. namespace reflection  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {            InitializeComponent();  
  16.         }  
  17.         private void Form1_Load(object sender, EventArgs e)  
  18.         {  
  19.             int n = 45;  
  20.             //string n = "vineet";  
  21.             System.Type t = n.GetType();  
  22.             MessageBox.Show(t.ToString());//Display type of varible  
  23.             MessageBox.Show(t.Name);//Display name of variable  
  24.             MessageBox.Show(t.Namespace);//Display namespace by name  
  25.             MessageBox.Show(t.IsArray.ToString());//Display boolean value  
  26.             MessageBox.Show(t.IsClass.ToString());//Display boolean value  
  27.             MessageBox.Show(t.Module.ToString());//Display module(library) of assembly  
  28.             MessageBox.Show(t.MemberType.ToString());//Display membertype by name  
  29.             MessageBox.Show(t.GetProperties().ToString());//Display property by name  
  30.             MessageBox.Show(t.GetType().ToString());//Display type defined in assembly  
  31.             MessageBox.Show(t.GetMethods().ToString());//Display method by name  
  32.             MessageBox.Show(t.GetInterfaces().ToString());//Display interface by name  
  33.             MessageBox.Show(t.FullName.ToString());//Display type of variable by name  
  34.             MessageBox.Show(t.BaseType.ToString());//Display base type  
  35.             MessageBox.Show(t.Attributes.ToString());//Display attribute by name  
  36.             MessageBox.Show(t.Assembly.ToString());//Display assembly by name  
  37.             MessageBox.Show(t.AssemblyQualifiedName.ToString());//Display AssemblyQualifiedName  
  38.             System.Reflection.Assembly o = System.Reflection.Assembly.Load("mscorlib.dll");  
  39.             MessageBox.Show(o.GetName().ToString());//Display information about  mscorlib assembly  
  40.             object obj = o.GetType();  
  41.             MessageBox.Show(obj.ToString());//Display name of runtime assembly  
  42.             ConstructorInfo[] ci = t.GetConstructors();//Display information about constructors  
  43.             foreach (ConstructorInfo i in ci)  
  44.             {  
  45.                 MessageBox.Show(i.ToString());  
  46.             }  
  47.             MethodInfo[] mi = t.GetMethods();//Display information about methods  
  48.             foreach (MethodInfo i in mi)  
  49.             {  
  50.                 MessageBox.Show(i.ToString());  
  51.             }  
  52.             PropertyInfo[] pi = t.GetProperties();//Display information about properties   
  53.             foreach (PropertyInfo i in pi)  
  54.             {  
  55.                 MessageBox.Show(i.ToString());  
  56.             }  
  57.             MemberInfo[] mf = t.GetMembers();//Display information about members  
  58.             foreach (MemberInfo i in mf)  
  59.             {  
  60.                 MessageBox.Show(i.ToString());  
  61.             }  
  62.             EventInfo[] ei = t.GetEvents();//Display information about events   
  63.             foreach (EventInfo i in ei)  
  64.             {  
  65.                 MessageBox.Show(i.ToString());  
  66.             }  
  67.             FieldInfo[] fi= t.GetFields();//Display information about fields  
  68.             foreach (FieldInfo i in fi)  
  69.             {  
  70.                 MessageBox.Show(i.ToString());  
  71.             }  
  72.         }  
  73.     }  
  74. }  

Summary

So reflection is very important for get the metadata information from assembly.