Hi, I am student of BSC.I.T. In last lecture we had got the lesson of late binding in c#. But I don't able to get it. Can anyone give me explanation about it in simple words.
Hi, I am student of BSC.I.T. In last lecture we had got the lesson of late binding in c#. But I don't able to get it. Can anyone give me explanation about it in simple words.
Hi, There are two types of Polymorphism. One is Static polymorphism and Dynamic polymorphism. Static polymorphism is also known as Early Binding and Dynamic polymorphism is also known as Late Binding.
Polymorphism is achieved as follow with the help of c#:
Code:class One { } class Two:One { } class Main { One oo=new Two(); }
Hi, you can make use of the Late Binding in C# with the help of Virtual function. When compiler finds virtual keyword in an function defination, while compiling, instead of binding to the function directly, it wait for the runtime to execute further. You will get it by taking look at following:Code:class base { protected virtual void PrintMessage() { Console.WriteLine("You are in Base Class"); } } class derived : base { protected override void PrintMessage() { Console.WriteLine("You are in Derived Class"); } } public static void Main() { base b = new base(); base d = new derived(); b.PrintMessage(); /*This will print:You are in Base Class*/ d.PrintMessage(); /* This will print:You are in Derived Class*/ }
Hi, I don't know how to achieve the late binding in C#, but while searching on internet I have got something which will create a console application to test DLLs with reflection and also it uses late binding to achieve this. Just check whether it is helpful to you or not.
Code:using System; using System.Reflection; using System.IO; static void Main() { string[] filenames = Directory.GetFiles(Environment.CurrentDirectory,CommonP2P.strSearchDLL); foreach (string filename in filenames) { Console.WriteLine("Load Protocol: {0}", filename); Assembly a = Assembly.LoadFrom(filename); Type[] types = a.GetTypes(); foreach (Type typ in types) { object obj = Activator.CreateInstance(typ); MethodInfo mi = typ.GetMethod("MyMsg"); mi.Invoke(0, null); } } }
Hi, Late Binding means compiler doesn't have any prior knowledge about the methods and properties of the class and it is delayed until runtime. So, basically programs don't know the addresses of methods and properties at compile time, it gets all those stuff at the time of execution, that is at the time of invocation of those particular method. Late Binding refers to only the generic data type. Most of the time late binding in C# is achieved through reflection. Reflection is a way to determine the type or information about the classes or interfaces.
Hi, Basically when compiler connects the method call to the method body is called as binding. So when this binding is done at the time of compiling it is termed as early binding. And if at the time of compilation compiler don't get the method body to which the method call has to be binded, it is called as late binding. As then that method is binded at run time. Late binding is also called dynamic binding or run-time binding. This mechanism varies from language to language.
Bookmarks