Monday, 5 March 2018

028 C# Reflection

Reflection
When .Net program is compiled , MSIL create a metedeta file , named manifest. Portable executable file or (PE)  store metadata + manifest information of  assembly. This can be access run time. This process is called reflection. PE  is generated when program is compiled in CLR. 

Portable executable file or (PE) has three part. 
  • PE header 
  • entry point 
  • code metadata
 In a nutshell, reflection is used to get type information during runtime. Reflection comes under the namespace System.Reflection.System.Reflection contain the class to retrieve the type information of existing object and dynamically added object. To retrieve the type information of an object, you need to create a instance of that type and binding it to the existing object.
Gettype() is the method to obtain the type information. Here is the list of type of information con be obtain from System.Reflection.
  • Module
  • Enum
  • MethodInfo
  • ConstructorInfo
  • MemberInfo
  • ParameterInfo
  • Type
  • FieldInfo
  • EventInfo
  • PropertyInfo 
  • Attributes
  • Enum
  • Event
  • Fields
Below is the example of  of how get type work

Example 1

using System;
namespace Application
{
       class school
      {

       }

      class Student
      {
            public static void Main()
            {
                  school obj = new school();
                 // Get the Type information.
                   Type objType = obj.GetType();
                 Console.WriteLine(objType.ToString());
                 Console.ReadKey();
            }
      }

}

Output





Below is the example how get information from reflection by creating instance of an existing class. The example shows how method information can be obtain and invoke method with argument. The example also so how property information can be obtained.

Example 1

using System;
using System.Reflection;

namespace Application
{
    class school
    {
       public int Id
        {
          get; set;
       }
      public void Getfees(int Param1 , string Param2)
      {
        Console.WriteLine("Value of Param1:" + Param1);
        Console.WriteLine("Value of Param2:" + Param2);
       }
}

class Student
{
          public static void Main()
          {
             school obj = new school();
            // Get the Type information.

             Type objType = obj.GetType();
             Console.WriteLine(objType.ToString());

             Console.WriteLine("*********************Method Information*************************");
             //Method Information
             MethodInfo myMethodInfo = objType.GetMethod("Getfees");

              //Method Name
              Console.WriteLine("Method Name :" +myMethodInfo.Name);

               //Method Attributes
                Console.WriteLine("Method Attributes :" + myMethodInfo.Attributes);

                //Method Invoke
                object[] arr = new object[] { 2, "Hellow World" };
                myMethodInfo.Invoke(obj, arr);

                 Console.WriteLine("********************Property Information********************");
                 //Get Property information
                 PropertyInfo[] propInfo = objType.GetProperties();

                 foreach (PropertyInfo pobj in propInfo)
                     {
                          Console.WriteLine(pobj.Name);
                     }

         Console.ReadKey();

                  }

           }

}

Output


 


Assembly 
System.Reflection.Assembly class  help us to load and define assembly and get information like assembly name, version, location ,versionable. Assembly class can read the manifest during run time. Below is the example how an assembly class can be used.

using System;
using System.Reflection;
namespace Application
{

class Student
{
         public static void Main()
         {
             var assembly = Assembly.GetExecutingAssembly();
             Console.WriteLine("Assembly Name: " + assembly.GetName().Name);
             Console.WriteLine("Version: " + assembly.GetName().Version.ToString());
             Console.WriteLine("Full Name of Assembly"+assembly.FullName.ToString());
             Console.WriteLine("Location of Assembly" + assembly.Location.ToString());
            Console.ReadKey();
         }
   }

}

Output :


Dynamically Create Instance of a Class at Runtime in C#

Reflection support activated create instance to create dynamic instance of a class. Below is the example of dynamically created  of  class.
using System;
using System.Reflection;

namespace Application
{
            class school
            {
              public void ShowData(string param1,string param2)
                {
                    Console.WriteLine(param1 + " " + param2);
                }
            }

            class Student
            {

                public static void Main()
                 {
                    Object obj = Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase,
                                                                                                            typeof(school).FullName);

                    Console.ReadKey();
                   }
            }
}

We can load dll or assembly file with the full path and the get assembly information. File will be loaded to assembly variable and you can use it .Belows the example of the same 

 
using System;
using System.Reflection;

namespace Application
{
                class school
                 {
                      public void ShowData(string param1,string param2)
                       {
                         Console.WriteLine(param1 + " " + param2);
                       }
                   }

                  class Student
                  {
                    public static void Main()
                      {
                                Assembly myAssembly = Assembly.LoadFile(@"D:\Reflection\ConsoleReflection   \reflection.dll");
                                Console.ReadKey();

                      }
}

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...