[IronPython] DLR Documentation?

Dino Viehland dinov at exchange.microsoft.com
Mon Sep 24 17:56:53 CEST 2007

Unfortunately we don't have any great sources of documentation.  The way you do any dynamic invocation is usually through a DynamicSite - which is the fastest way to do it.  There's also another way you could do this which is via ScriptEngine.CallObject - which is the simplest way to do it.  Ultimately we're still working on what the end story is for what the best way for doing these operations still from static languages.

So let's look at these two ways.  First we have the DynamicSite.  There's a couple of different forms of DynamicSite's but we'll keep things simple and look at the normal one.  You create a dynamic site like so:

        DynamicSite<object, object, object> ds = DynamicSite<object,object,object>.Create(CallAction.Simple);

This creates a DynamicSite which takes 2 parameters (typed to object) and returns a parameter typed to object.  The DynamicSite will attempt to perform a call operation on the 1st argument passing it the 2nd argument and returning the result.

So then to invoke a site we'd do:

object res = ds.Invoke(codeContext, callableObject, parameter);

The one problem here is where do you get a CodeContext object from?  My best advice to you now is if you're using IronPython objects just use IronPython.Runtime.Calls.DefaultContext.Default which is still public.  Again we're still working on this part of the story :).

So that will let you call something but one thing which is noticeably missing is that we never got "Foo".  That can be done with a GetMemberAction, so we get another site:

DynamicSite<object, object> gmSite = DynamicSite<object, object>.Create(GetMemberAction.Make("Foo"));

Now we combine the two:

object res = ds.Invoke(DefaultContext.Default, gmSite.Invoke(obj), parameter);

and now we've gotten the Foo member and invoked it.  Now the great thing about this is that this one code path will do what you want - it will work w/ a C# object, it will work w/ an IronPython object, and it'll work w/ a Ruby object.

Doing this via ScriptEngine is basically the same thing - except for using site's were calling the SE's methods.  So that becomes

object callable;
If(se.TryGetObjectMemberVariable(obj, "Foo",out callable)) {
        res = se.CallObject(callable, parameter);

Hopefully that will get you started.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Lee Culver
Sent: Sunday, September 23, 2007 10:37 PM
To: Discussion of IronPython
Subject: Re: [IronPython] DLR Documentation?

I should have been more specific here.  I *am* interested in general DLR information, but specifically there is a problem I am trying to solve, and I'm wondering if anyone has some advice on this:

I'd like to write a C# wrapper function which dynamically calls a method of a class.  For a general method in the .Net library I would do something like this (where obj is an "object"):

            Type t = obj.GetType();
            MethodInfo mi = t.GetMethod("Foo");
            result = mi.Invoke(obj, parameters);

(Just hacked this together, might be errors there).  What I'd like to do is have a second code path so my function could also handle objects from the DLR itself.  Is there a way of doing this for objects based on the DLR (e.g. IronPython 2.0)?  How do you check if a general "object" is a DLR object?

My goal is to create a function which will call Foo on the given object given no matter if the object is a C# object, an IronRuby object, IronPython object, etc.

From: users-bounces at lists.ironpython.com [users-bounces at lists.ironpython.com] On Behalf Of Lee Culver [leculver at microsoft.com]
Sent: Saturday, September 22, 2007 10:35 AM
To: Discussion of IronPython
Subject: [IronPython] DLR Documentation?

I took some time to play with IronPython 2.0 this weekend, and I see that the DLR is included with it.  I'm interested playing with some of the interface and looking to see how new languages might be implemented on the DLR.  Obviously I could dig through the source (and I have been), but I was wondering if there is any documentation available on using the DLR?  (I understand that it would be subject to change at any time, being early in development and all.)

Users mailing list
Users at lists.ironpython.com

More information about the Ironpython-users mailing list