« Microsoft buys Groove | Main | Support for VB6 is not ending »
March 11, 2005
C# Late Binding
With one line of C# code, you can invoke a method on an object using late binding. You might find this easier than the Reflection GetMethod Invoke dance.
The only catch, you have to reference Microsoft.VisualBasic. This gives you access to the LateCall and LateGet methods. LateCall lets you invoke a method. LateGet does the same thing, but gives you access to the return value.
Enjoy:
using System; using Microsoft.VisualBasic.CompilerServices; namespace LateCS { class Class1 { [STAThread] static void Main(string[] args) { object f = new Foo(); // No return LateBinding.LateCall(f, null, "Bar", new object[] {"Scott"}, null, null); // Return string z = string.Empty; z = (string)LateBinding.LateGet(f, null, "Bar", new object[] {"Scott"}, null, null); Console.WriteLine(z); } } public class Foo { public string Bar(string x) { return "Bork bork bork"; } } }
Posted on March 11, 2005 at 08:41 PM | Permalink
Comments
One interesting thing about this, though, is that in calling the VB helpers, you'll get VB semantics for stuff like conversions and overload resolution. In VB 2005, we'll also resolve overloaded conversion operators, etc. So it can be very useful, but you do need to be aware that things might work a bit different than they do in C#...
Posted by: Paul Vick | Mar 12, 2005 6:04:39 PM
The class is both deprecated and not intended for developer use:
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.compilerservices.latebinding.aspx
Posted by: Bill Sorensen | Jul 18, 2008 4:03:01 PM
This is such bad practice.
Posted by: NT | Aug 30, 2008 4:46:40 AM
Modified below to show using the advertised VB methods, rather than the depracated ones. Still needs the Microsoft.VisualBasic reference too.
Whether it's still bad practice of course depends on what you're doing with it.
Can't find a help page to tell me the tags that work on this weblog, so code is uglified.
using System;
using Microsoft.VisualBasic;
namespace LateCS {
class Class1 {
[STAThread]
static void Main(string[] args) {
object f = new Foo();
// No/unused return
Interaction.CallByName(f, "Bar", CallType.Method, new object[] { "Scott" });
// Return
string z = string.Empty;
z = (string)Interaction.CallByName(f, "Bar", CallType.Method, new object[] { "Scott" });
Console.WriteLine(z);
Console.ReadLine();
}
}
public class Foo {
public string Bar(string x) {
return "Bork bork bork";
}
}
}
Posted by: tom | Sep 25, 2008 5:09:33 AM
What if you want to create the object itself via late-binding? For example, instead of this...
object f = new Foo();
...suppose one wanted to write something like this...
object f = new "a_string_that_is_my_class_name_goes_here";
...so is something like THAT possible?
Please advise.
Thank you.
-- Mark Kamoski
Posted by: Mark Kamoski | Nov 9, 2009 1:03:24 PM
how is this any better than:
z = (string)f.GetType().InvokeMember("Bar", System.Reflection.BindingFlags.InvokeMethod, null, f, new object[] {"Scott"} );
Posted by: Lance | Feb 24, 2010 3:13:46 PM
The comments to this entry are closed.