How do you implement C#4's IDynamicObject interface?
Asked Answered
T

5

6

To implement "method-missing"-semantics and such in C# 4.0, you have to implement IDynamicObject:

public interface IDynamicObject
{
  MetaObject GetMetaObject(Expression parameter);
}

As far as I can figure out IDynamicObject is actually part of the DLR, so it is not new. But I have not been able to find much documentation on it.

There are some very simple example implementations out there (f.x. here and here), but could anyone point me to more complete implementations or some real documentation?

Especially, how exactly are you supposed to handle the "parameter"-parameter?

Tucker answered 29/10, 2008 at 6:38 Comment(0)
D
4

The short answer is that the MetaObject is what's responsible for actually generating the code that will be run at the call site. The mechanism that it uses for this is LINQ expression trees, which have been enhanced in the DLR. So instead of starting with an object, it starts with an expression that represents the object, and ultimately it's going to need to return an expression tree that describes the action to be taken.

When playing with this, please remember that the version of System.Core in the CTP was taken from a snapshot at the end of August. It doesn't correspond very cleanly to any particular beta of IronPython. A number of changes have been made to the DLR since then.

Also, for compatibility with the CLR v2 System.Core, releases of IronPython starting with either beta 4 or beta 5 now rename everything in that's in the System namespace to be in the Microsoft namespace instead.

Dayfly answered 29/10, 2008 at 13:0 Comment(0)
M
3

If you want an end to end sample including source code, resulting in a dynamic object that stores value for arbitrary properties in a Dictionary then my post "A first look at Duck Typing in C# 4.0" could be right for you. I wrote that post to show how dynamic object can be cast to statically typed interfaces. It has a complete working implementation of a Duck that is a IDynamicObject and may acts like a IQuack.

If you need more information contact me on my blog and I will help you along, as good as I can.

Midcourse answered 4/11, 2008 at 0:25 Comment(0)
H
2

I just blogged about how to do this here:

http://mikehadlow.blogspot.com/2008/10/dynamic-dispatch-in-c-40.html

Histrionics answered 30/10, 2008 at 9:49 Comment(1)
Whenever I post something like this that is just a link to an article (mire or someone else's) I get voted down.Panacea
T
0

Here is what I have figured out so far:

The Dynamic Language Runtime is currently maintained as part of the IronPython project. So that is the best place to go for information.

The easiest way to implement a class supporting IDynamicObject seems to be to derive from Microsoft.Scripting.Actions.Dynamic and override the relevant methods, for instance the Call-method to implement function call semantics. It looks like Microsoft.Scripting.Actions.Dynamic hasn't been included in the CTP, but the one from IronPython 2.0 looks like it will work.

I am still unclear on the exact meaning of the "parameter"-parameter, but it seems to provide context for the binding of the dynamic-object.

Tucker answered 29/10, 2008 at 12:39 Comment(0)
T
0

This presentation also provides a lot of information about the DLR:

Tucker answered 29/10, 2008 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.