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;
-
- System.Type t = n.GetType();
- MessageBox.Show(t.ToString());
- MessageBox.Show(t.Name);
- MessageBox.Show(t.Namespace);
- MessageBox.Show(t.IsArray.ToString());
- MessageBox.Show(t.IsClass.ToString());
- MessageBox.Show(t.Module.ToString());
- MessageBox.Show(t.MemberType.ToString());
- MessageBox.Show(t.GetProperties().ToString());
- MessageBox.Show(t.GetType().ToString());
- MessageBox.Show(t.GetMethods().ToString());
- MessageBox.Show(t.GetInterfaces().ToString());
- MessageBox.Show(t.FullName.ToString());
- MessageBox.Show(t.BaseType.ToString());
- MessageBox.Show(t.Attributes.ToString());
- MessageBox.Show(t.Assembly.ToString());
- MessageBox.Show(t.AssemblyQualifiedName.ToString());
- System.Reflection.Assembly o = System.Reflection.Assembly.Load("mscorlib.dll");
- MessageBox.Show(o.GetName().ToString());
- object obj = o.GetType();
- MessageBox.Show(obj.ToString());
- ConstructorInfo[] ci = t.GetConstructors();
- foreach (ConstructorInfo i in ci)
- {
- MessageBox.Show(i.ToString());
- }
- MethodInfo[] mi = t.GetMethods();
- foreach (MethodInfo i in mi)
- {
- MessageBox.Show(i.ToString());
- }
- PropertyInfo[] pi = t.GetProperties();
- foreach (PropertyInfo i in pi)
- {
- MessageBox.Show(i.ToString());
- }
- MemberInfo[] mf = t.GetMembers();
- foreach (MemberInfo i in mf)
- {
- MessageBox.Show(i.ToString());
- }
- EventInfo[] ei = t.GetEvents();
- foreach (EventInfo i in ei)
- {
- MessageBox.Show(i.ToString());
- }
- FieldInfo[] fi= t.GetFields();
- foreach (FieldInfo i in fi)
- {
- MessageBox.Show(i.ToString());
- }
- }
- }
- }
Summary
So reflection is very important for get the metadata information from assembly.