{"id":413,"date":"2014-04-27T13:16:49","date_gmt":"2014-04-27T13:16:49","guid":{"rendered":"http:\/\/praveenkatiyar.wordpress.com\/?p=413"},"modified":"2014-04-27T13:16:49","modified_gmt":"2014-04-27T13:16:49","slug":"understanding-late-binding","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2014\/04\/27\/understanding-late-binding\/","title":{"rendered":"Understanding Late binding"},"content":{"rendered":"<p>in simplest words, <strong>late binding is a technique<\/strong>, in which <strong>you can create an instance of a given type, and can invoke its methods, at runtime<\/strong>, <strong>without having knowledge of its existence at the compile time<\/strong>.<\/p>\n<p><strong>System.Activator<\/strong> class plays a pivotal role in late binding process. <\/p>\n<p>let us understand, it by creating<\/p>\n<ul>\n<li>simple a Class library \u201c<strong>MyMathLib<\/strong>\u201d which exposes few methods let say <strong>Add, Subtract, Multiply<\/strong>. <\/li>\n<li>Console bases <strong>client program <\/strong>, that will use this library using the conventional (early binding) method. <\/li>\n<li>Console bases <strong>client program<\/strong> , that will use this library using the late binding method. <\/li>\n<\/ul>\n<h3>Creating the class library<\/h3>\n<p>Create a Class library, let\u2019s name it \u201c<strong>MyMathLib<\/strong>\u201d choosing the \u201c<strong>Class Library<\/strong>\u201d template from the project wizard. Default project is created with a public class <strong>Class1 <\/strong>defined in <strong>Class1.cs. <\/strong><\/p>\n<h4>Little house keeping.<\/h4>\n<p>rename the <strong>Class1 as \u201cMathClass\u201d<\/strong> using the refactor menu.<\/p>\n<p>rename the <strong>class1.cs as MathClass.cs<\/strong><\/p>\n<p>Now define methods <strong>Add, Subtract<\/strong> and <strong>Multiply <\/strong>for <strong>MathClass <\/strong>as shown below.&#160; as shown below.<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">using System;      <br \/>using System.Collections.Generic;       <br \/>using System.Linq;       <br \/>using System.Text;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">namespace MyMathLib      <br \/>{       <br \/>&#160;&#160;&#160; public class MathClass       <br \/>&#160;&#160;&#160; {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; public double Add(double dblX, double dblY)       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (dblX + dblY);       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; public double Subtract(double dblX, double dblY)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (dblX &#8211; dblY);       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; public double Multiply(double dblX, double dblY)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (dblX * dblY);       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }       <br \/>&#160;&#160;&#160; }       <br \/>}<\/font><\/p>\n<p>&#160;<\/p>\n<p>Compile and Build library. <\/p>\n<h3>Creating the Application, that will use this library<\/h3>\n<blockquote>\n<p>Create a Console application, let\u2019s name it \u201c<strong>TestEarlyBinding<\/strong>\u201d choosing the \u201c<strong>Console Application<\/strong>\u201d template from the project wizard. Default project is created. <\/p>\n<h4>Little house keeping.<\/h4>\n<\/blockquote>\n<p><strong>Add Reference of MathLib class library project, created earlier.<\/strong><\/p>\n<p>Write code in the main function to use this library as shown below.<\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">using System;     <br \/>using System.Text;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>using MyMathLib;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">namespace TestEarlyBinding     <br \/>{      <br \/>&#160;&#160;&#160; class Program      <br \/>&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; static void Main(string[] args)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblResult = 0;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblX = 200; double dblY = 200;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Tesing early binding&quot;);&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong><font size=\"2\"> MathClass mathObj = new MyMathLib.MathClass ();<\/font>        <br \/><\/strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = <strong>mathObj<\/strong>.Add(dblX, dblY);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Adding {0} and {1} results to {2}&quot;, dblX, dblY, dblResult);<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = <strong>mathObj<\/strong>.Subtract (dblX, dblY);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Adding {0} and {1} results to {2}&quot;, dblX, dblY, dblResult);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = <strong>mathObj<\/strong>.Multiply(dblX, dblY);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Adding {0} and {1} results to {2}&quot;, dblX, dblY, dblResult);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br \/>&#160;&#160;&#160; }      <br \/>}<\/font><\/p>\n<p>Compile and build and here is the output.<\/p>\n<p><a href=\"https:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2014\/04\/image.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"https:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2014\/04\/image_thumb.png\" width=\"454\" height=\"59\" \/><\/a><\/p>\n<p>till now, it was default way, we used to do things, but this post is about late binding. <\/p>\n<p>so using the same class library&#160; with late binding, let\u2019s create one more console based project let\u2019s&#160; call it <strong>TestLateBidning.<\/strong><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">using System;     <br \/>using System.Text;      <br \/>using System.Reflection;<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">namespace TestLateBidning     <br \/>{      <br \/>&#160;&#160;&#160; class Program      <br \/>&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; static void Main(string[] args)      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblResult = 0;      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double dblX = 200; double dblY = 100;<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Tesing late binding&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><font face=\"Courier New\"><font color=\"#0000ff\"><strong>Assembly assem = null;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/strong>Console.WriteLine(&quot;Loading MyMathLib assembly . . .&quot;);        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; try        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; assem = Assembly.Load(&quot;MyMathLib&quot;);        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; catch (Exception eX)        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Error while loading MyMathLib Error [{0}] \\n\\nMyMathLib.dll not found in application Folder&quot;, eX.Message);        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/font><\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Creating Instance of MathClass object . . .&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Type mathType = assem.GetType(&quot;MyMathLib.MathClass&quot;);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; object mathObjLB = Activator.CreateInstance(mathType);<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Invoke method [Add] . . .&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; MethodInfo miAdd = mathType.GetMethod(&quot;Add&quot;);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = (double)miAdd.Invoke(mathObjLB, new object[] { 100, 200 });      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Adding {0} and {1} results to {2}&quot;, dblX, dblY, dblResult);<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Invoke method [Subtract] . . .&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; MethodInfo miSub = mathType.GetMethod(&quot;Subtract&quot;);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = (double)miSub.Invoke(mathObjLB, new object[] { 100, 200 });      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Subtracting {0} and {1} results to {2}&quot;, dblX, dblY, dblResult);<\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Invoke method [Multiply] . . .&quot;);     <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; MethodInfo miMul = mathType.GetMethod(&quot;Multiply&quot;);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult = (double)miMul.Invoke(mathObjLB, new object[] { 100, 200 });      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Multiply {0} and {1} results to {2}&quot;, dblX, dblY, dblResult);      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br \/>&#160;&#160;&#160; }      <br \/>}<\/font><\/p>\n<h4>Explanation:<\/h4>\n<p><font color=\"#0000ff\"><strong>Assembly assem = null;       <br \/><\/strong>Console.WriteLine(&quot;Loading MyMathLib assembly . . .&quot;);      <br \/>try      <br \/>{      <br \/>&#160;&#160;&#160; assem = Assembly.Load(&quot;MyMathLib&quot;);      <br \/>}      <br \/>catch (Exception eX)      <br \/>{<\/font><\/p>\n<p><font color=\"#0000ff\">&#160;&#160;&#160; . . . . .&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160; return<\/font><\/p>\n<p><font color=\"#0000ff\">}<\/font><\/p>\n<p>Assembly class represents an assembly, which is a valid building block of a common language runtime. <\/p>\n<p>Load method simply loads an assembly, whose name has been provided. <\/p>\n<p><font color=\"#0000ff\">Type mathType = assem.GetType(&quot;MyMathLib.MathClass&quot;);     <br \/>object <font color=\"#ff0000\"><strong>mathObjLB<\/strong><\/font> = Activator.CreateInstance(mathType);<\/font><\/p>\n<p>Activator class contains methods to create types of object of the specified type, locally or remotely.&#160; <\/p>\n<p><font color=\"#0000ff\">MethodInfo <font color=\"#9b00d3\"><strong>miAdd<\/strong><\/font> = mathType.GetMethod(&quot;Add&quot;);<\/font><\/p>\n<p><strong>GetMethod<\/strong> is member function of Type class, that searches the given method name, and returns the <strong>MethodInfo, <\/strong><strong>MethodInfo <\/strong>class discovers the attributes of a method access its metadata.<\/p>\n<p>dblResult = (double)<font color=\"#9b00d3\"><strong>miAdd<\/strong><\/font>.Invoke(mathObjLB, new object[] { 100, 200 });(<strong><font color=\"#ff0000\">mathObjLB<\/font><\/strong>, new object[] { 100, 200 });<\/p>\n<p>Invoke method of <strong>MethodInfo<\/strong> class simply calls the method, using the required parameters.<\/p>\n<p>similarly other methods (Subtract, Multiply) are discovered and invoked.<\/p>\n<p>build and execute.<\/p>\n<p><a href=\"https:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2014\/04\/image1.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"https:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2014\/04\/image_thumb1.png\" width=\"742\" height=\"89\" \/><\/a><\/p>\n<p>oops, there was an error, as it could not found the<strong> MyMathLib.dll<\/strong> in the test application executable (<strong>TestLateBinding.exe<\/strong>) folder.<\/p>\n<p>just copy the <strong>MyMathLib.dll<\/strong> to the test application\u2019s executable folder and execute the client again. and you will see what was expected.<\/p>\n<p><a href=\"https:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2014\/04\/image2.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top:0;border-right:0;background-image:none;border-bottom:0;padding-top:0;padding-left:0;border-left:0;display:inline;padding-right:0;\" border=\"0\" alt=\"image\" src=\"https:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2014\/04\/image_thumb2.png\" width=\"548\" height=\"141\" \/><\/a><\/p>\n<p>&#160;<\/p>\n<p>Hope it useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>in simplest words, late binding is a technique, in which you can create an instance of a given type, and can invoke its methods, at runtime, without having knowledge of its existence at the compile time. System.Activator class plays a pivotal role in late binding process. let us understand, it by creating simple a Class&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2014\/04\/27\/understanding-late-binding\/\">Continue reading <span class=\"screen-reader-text\">Understanding Late binding<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,7],"tags":[17,27],"class_list":["post-413","post","type-post","status-publish","format-standard","hentry","category-codeproject","category-general","tag-net","tag-late-binding","entry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=413"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/413\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}