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#?
- Automation Using Selenium in C# With Example
- How to Execute C# Program on cmd (command-line)?
- How to Install and Setup Visual Studio for C#?
- What is .NET 3-Tier Architecture?
- Cross Join in LINQ
- Comparing two ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> in C#
- Compare two ValueTuple<T1, T2, T3, T4, T5, T6, T7> in C#
- Comparing two ValueTuple<T1, T2, T3, T4, T5, T6> in C#
- Comparing two ValueTuple<T1, T2, T3, T4, T5> in C#
- Comparing two ValueTuple<T1, T2, T3, T4> in C#
- Comparing two ValueTuple<T1, T2, T3> in C#
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.