Serving Information Simply

Wednesday 13 March 2013

Reflection in C#


It is a powerful  way of collecting and manipulate information present in
Application’s assemblies and its metadata. Metadata contain all the Type
Information used by the application.

What is Reflection?

Reflection is the ability to find out information about objects, the
application details (assemblies), its metadata at run-time.

How to use Reflection in our applications?

Namespace :- System.Reflection
The Type class Base for all reflection.

Classes:-

Assembly Class: Assembly Class can be used to get the information and
Manipulate the assembly.

Module Class: Module Class is used for the reflection of the module.
This class is used to get information about parent assembly, classes in the module, all the global methods available in the classes.

ConstructorInfo Class:It is used for the reflection of a constructor, and used to fetch the information about constructor also constructor can be invoked.
Constructor information like name, parameters, access modifiers, and various implementation details (such as abstract or virtual) can be obtained.

MethodInfo Class : It is used to obtain information such as the name, return type, parameters, access modifiers (such as public or private), and implementation details  (such as abstract or virtual) of a any method inside the class, module,  assembly. GetMethods or GetMethod method of a Type are used to on the to obtain the information.

fieldInfo Class
EventInfo Class
PropertyInfo Class
ParameterInfo Class

eg:-private void Form1_Load(object sender, EventArgs e)
        {
            int i = 42;

            System.Type type = i.GetType();
            MessageBox.Show(type.ToString());
            MethodInfo[] t= type.GetMethods();
            MessageBox.Show(t[0].ToString()); 
 
  Assembly x=Assembly.LoadFrom(@"C:\Documents and Settings\abhinav\My Documents\Visual Studio
2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe");

  MessageBox.Show(x.FullName);
  MessageBox.Show(x.CodeBase);
   MessageBox.Show(x.EntryPoint.Name.ToString());
    MessageBox.Show(x.FullName.ToString());

  Type[] t = x.GetTypes();
   foreach (Type y in t)
          {
              MessageBox.Show(y.ToString());
          }
        Module[] p = x.GetModules();
          foreach (Module z in p)
          {
   MessageBox.Show(z.ToString());
          }

          Type gt = typeof(System.String);
          // Constructors.
          ConstructorInfo[] t9 = gt.GetConstructors(BindingFlags.Instance | BindingFlags.Public);
          foreach (MemberInfo memberInformationDetails in t9)
          {
              MessageBox.Show(memberInformationDetails.ToString());
          } 
        }

================

WindowsFormsApplication1.Form1 x = new WindowsFormsApplication1.Form1();
            Type t=x.GetType();
            MessageBox.Show(t.Assembly.FullName.ToString());
            MessageBox.Show(t.BaseType.Name.ToString());
            MethodInfo[] y=t.GetMethods();
            foreach (MethodInfo MI in y)
            {
                MessageBox.Show(MI.ToString());  
            }
            ConstructorInfo[] p1 = t.GetConstructors();
            foreach (ConstructorInfo CI in p1)
            {
                MessageBox.Show(ppp.ToString());
            }
---------------------------------------------------------------------------------------
 Thank you guys... keep visiting ….:)
If you like this post then please join this site and like it’s Facebook page for getting updates.

No comments:

Post a Comment