Reflection Concept and Late Binding in C#

Introduction

Reflection concept in C# is ability of inspecting metadata of assembly at runtime meaning assembly content is described by looking at the assembly metadata at run time namespace.

When it comes to use reflection in practice scenarios it is very confusing for the developers to use refection concept properly and get benefit for this beautiful feature.

By this article I am trying to explain refection concept and the particle uses in projects.

In simple terms refection is reflection provides objects that encapsulate assemblies, modules and types meaning reflection provides a way for dynamic creation of an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties

Reflection

Reflection is the ability of a managed code to read its own metadata for the purpose of finding assemblies, modules and type information at runtime. The classes that give access to the metadata of a running program are in System.Reflection.

System.Reflection namespace defines the following types to analyze the module's metadata of an assembly: Assembly, Module, Enum, ParameterInfo, MemberInfo, Type, MethodInfo, ConstructorInfo, FieldInfo, EventInfo, and PropertyInfo.

Reflection provides objects that encapsulate assemblies, modules and types.

Assembly Metadata

I am giving a quick view about an assembly which is a logical DLL or EXE which is described by manifest which is nothing but a detailed description (metadata) of an assembly. The .NET compiler produces a portable executable PE file for CLR with the extensions of .exe or .dll. This PE file is mainly comprised of metadata and IL (Intermediate Language).

Explanation:

Whenever a .net project is build the code will be get complied into Intermediate language and then packaged in assembly. An assembly contains metadata which has the information about the type of classes defined meaning it contains all the required information about the members, fields, constructors, properties, methods, function etc used in the class for building the package.

By using refection an assembly can be inspected.

  1. Type _type = Type.GetType("ReflectionConcept.Employee");  
  2. Or  
  3. Type _type = typeof(Employee); 


Assembly Metadata

Sample Code

  1. namespace ReflectionConcept  
  2. {  
  3.     public class Employee  
  4.     {  
  5.         public int empID { getset; }  
  6.         public string empName { getset; }  
  7.         public float Salary { getset; }  
  8.   
  9.         public Employee()  
  10.         {  
  11.             this.empID = -1;  
  12.             this.empName = string.Empty;  
  13.             this.Salary = 0;  
  14.         }  
  15.   
  16.         public Employee(int id, string name, float salary)  
  17.         {  
  18.             this.empID = id;  
  19.             this.empName = name;  
  20.             this.Salary = salary;  
  21.         }  
  22.   
  23.         public void displayName()  
  24.         {  
  25.             Console.WriteLine("Name :" + this.empName);  
  26.         }  
  27.   
  28.         public void printSalary()  
  29.         {  
  30.             Console.WriteLine("Salary :" + this.Salary);  
  31.         }  
  32.   
  33.     }  
  34.   
  35.     class Program  
  36.     {  
  37.         static void Main(string[] args)  
  38.         {  
  39.            Type _type = Type.GetType("ReflectionConcept.Employee");  
  40.   
  41.            // Type _type = typeof(Employee);  
  42.   
  43.   
  44.             Console.WriteLine(_type.FullName);  
  45.             Console.WriteLine(_type.Namespace);  
  46.             Console.WriteLine(_type.Name);  
  47.   
  48.             PropertyInfo[] info  = _type.GetProperties();  
  49.   
  50.             foreach (PropertyInfo propinfo in info)  
  51.             {  
  52.                 Console.WriteLine(propinfo.Name);  
  53.             }  
  54.   
  55.             MethodInfo [] methods=_type.GetMethods();  
  56.   
  57.             foreach (MethodInfo methodinfo in methods)  
  58.             {  
  59.                 Console.WriteLine(methodinfo.ReturnType.Name);  
  60.                 Console.WriteLine(methodinfo.Name);  
  61.             }  
  62.   
  63.             Console.ReadLine();  
  64.         }  
  65.   
  66.           
  67.     }  

Uses of Reflections

A. Refection is very heavily used by IDE developers or UI designers. Property window in .net application uses refection to show list of properties.

Still confused….alright let me take an example of web application.

In web application whenever developers drags and drop a control from control list to form. Here reflection plays the role to provide way to dynamically display all the properties of a label and these properties can be changed as per the requirement. So this concept is very important and useful for the

Uses of Reflections

B. Late binding can be achieved by using refection meaning it gives developer a way to use code that is not available at compile time. By using refection instance of type can be created dynamically which don’t have information at compile time.

Important Sticky:

  • It allows view attribute information at runtime.
  • It allows examining various types in an assembly and instantiate these types.
  • It allows late binding to methods and properties
  • It allows creating new types at runtime and then performs some tasks using those types.

Late binding using reflections

Before learning late binding let us have look for the early binding.

Early Binding can be explained by the situation where the compiler knows about what kind of objects it is , what all the methods are and properties it contains.

In the example at compiler time developer has the information about the Employee class and all attributes of the class. So here in early binding we just need to create instance of the Employee class and methods, properties with that class can be used easily.

  1. Employee emp = new Employee();  
  2.   emp.displayName(); 

Late binding is nothing but binding at run time. Compiler does not know what kind of object it is, what are all the methods and properties it contains.

Late binding is achieved by using reflections.

Sample Code

  1. namespace ReflectionConcept  
  2. {  
  3.     public class Employee  
  4.     {  
  5.         public int empID { getset; }  
  6.         public string empName { getset; }  
  7.         public float Salary { getset; }  
  8.   
  9.         public Employee()  
  10.         {  
  11.             this.empID = -1;  
  12.             this.empName = string.Empty;  
  13.             this.Salary = 0;  
  14.         }  
  15.   
  16.         public Employee(int id, string name, float salary)  
  17.         {  
  18.            this.empID = id;  
  19.             this.empName = name;  
  20.             this.Salary = salary;  
  21.         }  
  22.   
  23.         public void displayName()  
  24.         {  
  25.             Console.WriteLine("Name :" + this.empName);  
  26.         }  
  27.   
  28.         public void displayemp(string name)  
  29.         {  
  30.             Console.WriteLine("Name :" + name);  
  31.         }  
  32.   
  33.   
  34.         public void printSalary()  
  35.         {  
  36.             Console.WriteLine("Salary :" + this.Salary);  
  37.         }  
  38.   
  39.     }  
  40.   
  41.     class Program  
  42.     {  
  43.         static void Main(string[] args)  
  44.         {  
  45.             Assembly exceutable = Assembly.GetExecutingAssembly();  
  46.             Type Ltype =exceutable.GetType("ReflectionConcept.Employee");  
  47.   
  48.             object objEmployee = Activator.CreateInstance(Ltype);  
  49.   
  50.             MethodInfo method = Ltype.GetMethod("displayemp");  
  51.   
  52.             string[] parameter = new string[1];  
  53.   
  54.             parameter[0] = "Abhishek";  
  55.   
  56.            string employeeName=   (string)method.Invoke(objEmployee, parameter);  
  57.   
  58.                 Console.ReadLine();  
  59.         }  
  60.           
  61.     }  

Early Binding Vs Late Binding

  • Performance will be good in early binding.
  • Application will run faster in Early binding, since no boxing or unboxing is done here but in late binding we need type conversion as it will be decoded at run time.
  • Easier to write the code in Early binding, since the intelligence will be automatically populated
  • Less chances errors in early binding, as syntax is checked which flags error at compile time itself but for late binding there are chances of run time exceptions.
  • Late binding would support in all kind of versions, due to handing at run time.
  • Minimal Impact of code in future enhancements, if Late Binding is used.

Conclusion

Above we have compared early binding and late binding features and found late binding is complex need more lines of code, venerable to run time exceptions , have issues with performance, no intelligence by .net.

So it is very important to understand where late binding should be used.

Answer would be when we are working with objects that are not available at compile time and so this can be achieved using reflections but recommended to use if you have a scenario in the project where you don’t have required information about the object available at compile time.

Next Recommended Readings