-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: SitaramanM
Message 2 in Discussion
Hi Late binding is there in C# or Not? That was a good one. C# does not support
Late Binding . That said, this has been a topic that i was exploring a couple of
weeks back and found something extremely interesting. I have dilly-dallying about
doing a write-up on this, but never could pull myself to do it. now that you have
asked this question, here goes Late Binding is a feature which is available in
VB.Net only. C# does not support it. I always thought that LateBinding is supported
intrinsicly into the VB Language and is tightly couple within the language itself.
look at the following example. Im having a MyClass1 which has a function fn1. In the
module im declaring a Object type vaiable and instantiating it with MyClass1 .
Right!! Now I have two ways of calling the fn1 through the object. One is the
Reflection[subCallFunctionThroughReflection()] and another is Late
Binding[subCallFunctionThroughLateBinding() ]. here Refelection is a mechanism
which i can use both in VB.Net and C#. In fact as C# does not support Late Binding,
the only way to call such late-bound methods is through the alternative mechanism,
which is Reflection. Or..... is that so?????? we will see in a short while...
However in VB.Net, i can use Late Binding as shown below. Imports System.Reflection
Module Module1
Dim m_objGenericObject As Object 'Declaring a base object
Sub Main() m_objGenericObject = New MyClass1 ' Allocation of memory
subCallFunctionThroughReflection() ' Call Through Reflection
subCallFunctionThroughLateBinding() ' Call Through Reflection
Console.ReadLine()
End Sub
Private Sub subCallFunctionThroughReflection()
Dim l_objMethodInfo As MethodInfo
l_objMethodInfo = m_objGenericObject.GetType.GetMethod("fn1")
l_objMethodInfo.Invoke(m_objGenericObject, Nothing)
End Sub Private Sub subCallFunctionThroughLateBinding()
m_objGenericObject.fn1() 'Late-Bound Call
End Sub End Module
Public Class MyClass1 Public Function fn1()
Console.WriteLine("MyClass1.fn1 Called...")
End Function
End Class
Here im declaring an object, initialize it using MyClass1 and calling fn1,
which will be a Late-Bound Call. Right. how does VB handle this LateBinding?????
A look at the ILDASM will give a gr8 insight Private Shared Sub
subCallFunctionThroughLateBinding()
.maxstack 8
L_0000: nop
L_0001: ldsfld Module1.m_objGenericObject
L_0006: ldnull
L_0007: ldstr "fn1"
L_000c: ldc.i4.0
L_000d: newarr Object
L_0012: ldnull
L_0013: ldnull
L_0014: call LateBinding.LateCall
L_0019: nop
L_001a: nop
L_001b: ret Here look at the line marked in Red bold. What happens is that
when the vb code is compiled to the IL, what the compiler does is that, the line
x.fn1() 'Late-Bound Call is replaced by a LateBinding.LateCall function. LateCall
is a function of the LateBinding class which is present in the
Microsoft.VisualBasic.CompilerServices namespace(a handy namespace with a LOT of juicy
utility classes!!!!!!!!!). The vb code equivalent of this will be something like
Private Shared Sub subCallFunctionThroughLateBinding()
LateBinding.LateCall(Module1.m_objGenericObject, Nothing, "fn1", New Object((0) -
1) {}, Nothing, Nothing)
End Sub So LateBinding is basically implemented in Vb by this small trick that he
vb compiler performs. Also the fact that this functionality is inside the
Microsoft.VisualBasic.CompilerServices namespace means that this is a functionality
that is core to VB only. As a an appendum, i had said before that as C# does not
support Late Binding, the only way to call the function fn1 throught he Object
m_objGenericObject was using Reflection. One more way is that i use the same
mechanism that VB Compiler does. That is i can use the
Microsoft.Visualbasic.CompilerServices namespace in my C# class and call the function
just like the compiler does. Sample is as follows
using System;
using System.Reflection;
//To use this you need to reference the Microsoft.VisualBasic Assembly
// Undortunately it does not show in the references dialogbox. So copy
// the
C:\WINNT\assembly\GAC\Microsoft.VisualBasic\7.0.5000.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.dll
// file to your bin and then reference it as a private assembly
using Microsoft.VisualBasic.CompilerServices;
namespace Dummy_LateBindingInCsharp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
Object m_objGenericObject= new MyClass1(); // Allocation of memory;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Class1 l_objClass1= new Class1();
// Call Through Reflection
l_objClass1.subCallFunctionThroughReflection(); // Call Through Reflection
l_objClass1.subCallFunctionThroughSimulatedLateBinding(); Console.ReadLine();
} private void subCallFunctionThroughSimulatedLateBinding()
{
//m_objGenericObject.fn1(); //Late-Bound Call WILL NOT WORK
Object[] l_objParams=new Object[0]; // Need this do not pass a null object to the
LateCall Function
LateBinding.LateCall(m_objGenericObject, null, "fn1",l_objParams, null, null);
}
private void subCallFunctionThroughReflection()
{
MethodInfo l_objMethodInfo;
l_objMethodInfo = m_objGenericObject.GetType().GetMethod("fn1");
l_objMethodInfo.Invoke(m_objGenericObject, null);
}
} class MyClass1
{
public void fn1()
{
Console.WriteLine("MyClass1.fn1 Called...");
}
}
}
Here Check out the subCallFunctionThroughSimulatedLateBinding function. What i
have done here is the same as what the VB compiler does when you use LateBinding in
VB. So to summarise a) Calling methods through reflection is supported in both
VB.Net and C# b) LateBinding is supported only in VB.Net c) It is supported by a
mechanism, where the LateBound Calls are replaced by the compiler with a call to
LateBinding.LateCall function call d) C# implicitly does not support LateBinding e)
However by mimicking the work done by the VB Compiler, that is, making a call to
LateBinding.LateCall we can simulate LateBinding in C# also hth
regards, sr
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you received
this message by mistake, please click the "Remove" link below. On the pre-addressed
e-mail message that opens, simply click "Send". Your e-mail address will be deleted
from this group's mailing list.
mailto:[EMAIL PROTECTED]