Late-binding in .NET

0.00 avg. rating (0% score) - 0 votes

Early-binding:

Variable types are defined at compile time (e.g. string, integers):

String st = “hello world”;
Boolean ok = st.Contains(“hello”);

The compiler and linker will help verify that the argument types used to call a function matches the function’s signature.

Late-binding:

At compile time, variable are declared as generic type (Object in .NET or Variant in VB6). The actual type will only be defined later at run-time, just before the object is used.

The developer needs to specify the function signatures and to ensure that the correct types are used.

In C/C++, late binding (a.k.a. run-time dynamic linking) often takes the form of LoadLibrary / GetProcAddress / FreeLibrary. The MSDN Library provides a good example of late binding in C/C++. Early biding is known as load-time dynamic linking in C++.

In VB6 and VB.NET, late-binding is possible with Option Strict turn off:

Option Strict Off
Dim st as Object = “hello world”
Dim ok as Object = st.Contains(“hello”)

‘st’ will be resolved to String type and ‘ok’ to Boolean at run-time.

Option Strict is by default off for all new VB.NET project created in VS2005. To change the default to On, go to the Visual Studio’s Tools | Options menu, click the Projects VB Defaults folder and set Option Strict to On.

However, in C#, late-binding needs to be performed via Reflection:

Object obj = new object();
obj = “Hello world”;
Type objType = obj.GetType();
Object objRet = objType.InvokeMember(“IndexOf”, BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, obj, new object[] { “world” });

Reference:

  1. What is late binding?, http://blogs.msdn.com/davidklinems/archive/2006/11/27/what-is-late-binding.aspx
  2. Late Binding with Reflection, http://www.c-sharpcorner.com/UploadFile/samhaidar/LateBindingWithReflection09122005053810AM/LateBindingWithReflection.aspx?ArticleID=921b6d13-f609-4520-8a7d-8e70ce13b53b
0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>