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 .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace reflection
{
    public partial class Form1 : Form
    {
        public Form1()
        {            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            int n = 45;
            //string n = "vineet";
            System.Type t = n.GetType();
            MessageBox.Show(t.ToString());//Display type of varible
            MessageBox.Show(t.Name);//Display name of variable
            MessageBox.Show(t.Namespace);//Display namespace by name
            MessageBox.Show(t.IsArray.ToString());//Display boolean value
            MessageBox.Show(t.IsClass.ToString());//Display boolean value
            MessageBox.Show(t.Module.ToString());//Display module(library) of assembly
            MessageBox.Show(t.MemberType.ToString());//Display membertype by name
            MessageBox.Show(t.GetProperties().ToString());//Display property by name
            MessageBox.Show(t.GetType().ToString());//Display type defined in assembly
            MessageBox.Show(t.GetMethods().ToString());//Display method by name
            MessageBox.Show(t.GetInterfaces().ToString());//Display interface by name
            MessageBox.Show(t.FullName.ToString());//Display type of variable by name
            MessageBox.Show(t.BaseType.ToString());//Display base type
            MessageBox.Show(t.Attributes.ToString());//Display attribute by name
            MessageBox.Show(t.Assembly.ToString());//Display assembly by name
            MessageBox.Show(t.AssemblyQualifiedName.ToString());//Display AssemblyQualifiedName
            System.Reflection.Assembly o = System.Reflection.Assembly.Load("mscorlib.dll");
            MessageBox.Show(o.GetName().ToString());//Display information about  mscorlib assembly
            object obj = o.GetType();
            MessageBox.Show(obj.ToString());//Display name of runtime assembly
            ConstructorInfo[] ci = t.GetConstructors();//Display information about constructors
            foreach (ConstructorInfo i in ci)
            {
                MessageBox.Show(i.ToString());
            }
            MethodInfo[] mi = t.GetMethods();//Display information about methods
            foreach (MethodInfo i in mi)
            {
                MessageBox.Show(i.ToString());
            }
            PropertyInfo[] pi = t.GetProperties();//Display information about properties
            foreach (PropertyInfo i in pi)
            {
                MessageBox.Show(i.ToString());
            }
            MemberInfo[] mf = t.GetMembers();//Display information about members
            foreach (MemberInfo i in mf)
            {
                MessageBox.Show(i.ToString());
            }
            EventInfo[] ei = t.GetEvents();//Display information about events
            foreach (EventInfo i in ei)
            {
                MessageBox.Show(i.ToString());
            }
            FieldInfo[] fi= t.GetFields();//Display information about fields
            foreach (FieldInfo i in fi)
            {
                MessageBox.Show(i.ToString());
            }
        }
    }
}

Summary

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

Ebook Download
View all
Learn
View all