The two main terms appearing in the above topic are Late Binding and refection. So let us first define these two terms. The binding of methods and objects during run time is called Late Binding or Dynamic Binding. Reflection is the ability of an assembly to inspect its metadata. Metadata contains the information of the data within the assembly.
Reflection is used to implement late binding as it allows you to use code that is not available at compile time. We will see the examples of this in the following code segments.
Example 1 : In this program, we make use of late binding because we do not have the knowledge of the class that has to be instantiated at compile time. Declare an object named executing of class Assembly and load the current Assembly using GetExecutingAssembly method. Next, we have to find the type of the class that has to be instantiated later i.e. Student. To do this use the method GetType on the object of Assembly class and store the type in another object called studentType. GetType method expects a String argument which is the complete name of the Class, LateBinding.Student in this case. Since the type is unknown at this point in the program we use the class Type to declare studentType. Now we create an instance of the studentType using the CreateInstance method that uses the type as a parameter. The Activator class contains methods to create types of objects locally or remotely or obtain references to existing remote objects. Next, create a MethodInfo object and use the GetMethod method to store the information. Pass the name of the method as an argument to GetMethod. To invoke GetDetails, use the MethodInfo object to call the Invoke method and pass studentObject as a parameter. And finally, display the details using a String det and also define the class.
Student Details : Roll Number - Name 1 - Lisa
Note: In the code above, if you do not define the class and build the code, it will build successfully since the binding is not done at compile time. But when you run it you will get an error as it will only be encountered at run time. The error message will be as follows;
Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: type
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at LateBinding.Program.Main(String args) in m:\Documents\Visual Studio 2012\Projects\LateBinding\LateBinding\Program.cs:line 19
Example 2: Another similar example is the code given below that has a Shape class and we are late binding the method shapeName.
Name of the Shape is Quadrilateral
Note: The disadvantage of Late Binding is that if you make any spelling error in the name of a method or class, the compiler will not identify it at compile time and therefore the code will build successfully. In most practical scenarios, Early Binding is used as opposed to Late Binding.
- Early and late binding in C#
- What is Reflection in C#?
- How to implement is functionality without using is keyword in C#
- C# | Swap two Strings without using third user defined variable
- C# | Using foreach loop in arrays
- Invoking an overloaded constructor using this keyword in C#
- C# | Multiple inheritance using interfaces
- How to access structure elements using Pointers in C#
- Writing to Excel Sheet Using EPPlus in C#
- Basic Database Operations Using C#
- Automation Using Selenium in C# With Example
- Basic CRUD (Create, Read, Update, Delete) in ASP.NET MVC Using C# and Entity Framework
- Adding Text with a Float number Using String.Format() Method in C#
- Getting the Total Number of Days in a Month Using If-else and Switch Statements in C#
- How to Get a Total Number of Days in a Month using built in Functions in C#?
- Program to swap numbers using XOR operator in C#
- C# - Indexers Using String as an Index
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.