Welcome to MSDN Blogs Sign in | Join | Help

Dynamic dispatch in C#

Charlie Calvert blogged about dynamic support in C# 4.0. I love this for two reasons. One it will enable dynamic language interop and COM automation interop in a much better way, and two it will use the DLR. So if you were writing COM automation interop before this is what you would have had to write:

Type myType = Type.GetTypeFromProgID("IMyLib.MyClass");
object obj = Activator.CreateInstance(myType);
object[] args = new object[2];
args[0] = "Hello";
args[1] = 3;
myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, null, args);

Now you will probably write something like this:

Type myType = Type.GetTypeFromProgID("IMyLib.MyClass");
dynamic
{
    object obj = Activator.CreateInstance(myType);
    obj.MyMethod("Hello", 3);
}

If they use the DLR this will probably generate a dynamic site underneath to invoke the method. Essentially the DLR will probably create a delegate which will in effect do the invoke. For a simple case of making one call this might not boost perf but if the same method is used with different params etc. we should see better perf. (For an explanation of dynamic sites read Martin's posts on the topic). (Again this is all conjecture - I don't know how the C# team is going to design it). But more than perf I think the value is in the ease of use. This should pave the way for interop with IronPython/ IronRuby.

I find the syntax a little cumbersome though. Creating and using the dynamic variables will have to be right next to each other. While that might make the code a lot more readable I doubt if it would be practical. I would definitely like to make calls to strongly typed-apis in-between. I prefer the other syntax that a lot of people in the comments section have suggested - annotate the object definition with a keyword or an attribute. Ofcourse that creates its own readability mess. It won't be readily apparent if a call is dyamic or static. But I would argue we already have a similar mess with extension methods. Maybe a different operator can be used for invocation?

Published Tuesday, February 05, 2008 1:32 AM by srivatsn
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Monday, February 04, 2008 11:26 PM by MSDN Blog Postings » Dynamic dispatch in C#

# MSDN Blog Postings » Dynamic dispatch in C#

Wednesday, February 06, 2008 12:51 AM by とりこらぼ。

# dynamic ですって?

dynamic ですって?

Thursday, February 07, 2008 2:03 PM by Pablo

# re: Dynamic dispatch in C#

I agree about the operator.

I like the obj..MyMethod("Hello", 3) idea.

Because its easy to remember

One . for static

two .. for Dynamic

I'm not so crazy about the dynamic block idea but hey its early and they should experiment with various ideas.

Thursday, July 03, 2008 4:36 PM by Justin Chase

# re: Dynamic dispatch in C#

I'm not a big fan of they dynamic block idea. I'd much rather see a dynamic variable type such as:

dynamic x = 0;

x.Bad(); // compiles but has runtime exception

or a dynamic calling syntax such as:

int x = 0;

x:Bad(); // : to indicate dynamic

Here is a blog post I wrote about it

http://www.justnbusiness.com/Blogs/CSharp_Optionally_Dynamic_Calls.aspx

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker