In C#, Late Binding is a technique in which we are able to create an instance of a given type and invoke its members(methods, properties, fields,….etc) at runtime without having hard-coded compile-time knowledge of its existence. When you are building an application that binds late to a type in an external assembly, we have no reason to set a reference to the assembly. Therefore, the caller’s manifest has no direct listing of the assembly.
By using Late Binding in C# you cannot determine the errors at compile time because compile time does not have any information about assembly type. By using early binding we can determine errors at compile time, rather than at runtime. Late binding plays a critical role to create extendable application.
We can create instance of particular Type by using Activator class in C#. By using this instance we can invoke its methods, properties, fields…etc. System.Activator plays main role in late binding process. CreateInstance() method of System.Activator class used to create the instance of particular Type as shown below.
Type employee = assem.GetType("AssemblyForLateBinding.Employee");
object obj = Activator.CreateInstance(employee);
We will discuss late binding in C# by creating simple assembly “AssemblyForLateBinding”. Open Visual Studio => Create Project => Select Class Library and name it as AssemblyForLateBinding. Rename class Class1.cs to Employee.cs and add below code.
namespace AssemblyForLateBinding
{
public class Employee
{
public int GetEmpId()
{
return 101;
}
public string GetEmpName()
{
return "John";
}
public double GetEmpSalary(int empId)
{
return 10000;
}
}
}
Build the project, you will get the AssemblyForLateBinding.dll file in bin/Debug folder.
Now create Console Application and name it as LateBindingAssemblyExp and copy AssemblyForLateBinding.dll file to LateBindingAssemblyExp/bin/Debug folder. Add below code in Program.cs class.
class Program
{
static void Main(string[] args)
{
Assembly assem = null;
try
{
assem = Assembly.Load("AssemblyForLateBinding");
}
catch (FileNotFoundException e)
{
Console.WriteLine(e.Message);
return;
}
Type employee = assem.GetType("AssemblyForLateBinding.Employee");
object obj = Activator.CreateInstance(employee);
Console.WriteLine("Created a {0} using late binding!", obj);
InvokeMethodsWithNoParam(obj, employee);
InvokeMethodsWithParam(obj, employee);
Console.ReadLine();
}
static void InvokeMethodsWithNoParam(object obj, Type employee)
{
// Invoke GetEmpName method.
MethodInfo mi1 = employee.GetMethod("GetEmpName");
string empName = mi1.Invoke(obj, null).ToString();
Console.WriteLine("Invoking GetEmpName() method and It's Value: " + empName);
// Invoke GetEmpId method.
MethodInfo mi2 = employee.GetMethod("GetEmpId");
int empId = Convert.ToInt32(mi2.Invoke(obj, null));
Console.WriteLine("Invoking GetEmpId() method and It's Value: " + empId.ToString());
}
static void InvokeMethodsWithParam(object obj, Type employee)
{
// Invoke GetEmpSalary method.
MethodInfo mi1 = employee.GetMethod("GetEmpSalary");
// Bind late to a method which taking parameters.
object[] empParam = new object[1];
empParam[0] = 100;
double empSal = Convert.ToDouble(mi1.Invoke(obj, empParam));
Console.WriteLine("Invoking GetEmpSalary(int empId) method and It's Value: " + empSal.ToString());
}
}
As shown above, first we loaded the AssemblyForLateBinding assembly by using Load() method of Assembly class, then we got the AssemblyForLateBinding.Employee type by using GetType() method of assembly class. Create instance for this type by using CreateInstance method of Activator class and pass this instance and type to InvokeMethodsWithNoParam(), InvokeMethodsWithParam() methods. InvokeMethodsWithNoParam() method is used to invoke the methods of Employee which has no parameters and InvokeMethodsWithParam() is used to invoke the methods of Employee which has parameters.
To invoke the method use Invoke() method of MethodInfo class. To pass the parameters to the method create array object type and pass this array to Invoke() method of MethodInfo class.
The output as shown below after invoking the methods by using Late Binding in C#.