How do you implement the IDynamicObject C # 4 interface?

To implement the "method-missing" semitics, etc. in C # 4.0, you need to implement IDynamicObject:

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

As far as I can understand, IDynamicObject is actually part of the DLR, so it is not new. But I could not find a lot of documentation there.

There are some very simple implementation examples (fx here and here ), but can someone point me to more complete implementations or some real documentation?

In particular, how exactly should you handle the parameter parameter?

+6
dynamic dynamic-language-runtime
source share
5 answers

The short answer is that MetaObject is responsible for creating the code that will run on the call site. The mechanism that he uses for this is LINQ expression trees that have been extended to DLR. Therefore, instead of starting with an object, it starts with an expression that represents the object, and ultimately it will need to return an expression tree that describes the action that needs to be taken.

When playing with this, please remember that the version of System.Core in CTP was taken from a snapshot in late August. This is not very suitable for any specific IronPython beta. Since then, a number of changes have been made with DLR.

In addition, for compatibility with CLR v2 System.Core, IronPython releases, starting with beta 4 or beta 5, now rename everything that is used in the Microsoft namespace instead of the System namespace.

+4
source share

If you want to get a sample from the end to the end, including the source code, the result is a dynamic object that stores the value for any properties in the dictionary, then my post "A First Look at Duck Typing in C # 4.0" may be right for you. I wrote this post to show how a dynamic object can be ported to statically typed interfaces. It has a full working implementation of Duck, which is IDynamicObject and can act like IQuack.

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

+3
source share
+2
source share

Here is what I have guessed so far:

Dynamic Language Runtime is currently supported as part of the IronPython project . So this is the best place for information.

The easiest way to implement a class that supports IDynamicObject seems to be to get Microsoft.Scripting.Actions.Dynamic and override the appropriate methods, such as the Call-method to implement the semantics of function calls. It looks like Microsoft.Scripting.Actions.Dynamic was not included in CTP, but one of IronPython 2.0 looks like it will work.

I still have unclearly understood the exact meaning of the parameter-parameter, but it seems to provide a context for binding a dynamic object.

0
source share

This presentation also contains a lot of information about DLR:

0
source share

All Articles