[Ironpython-users] (Resend after group email address changed): Way to extend IronPython to application specific request without changing its source code?

Dino Viehland dinov at microsoft.com
Thu Jun 16 19:17:34 CEST 2011


You can implement IDynamicMetaObjectProvider on the object, then implement a DynamicMetaObject which overrides GetMember and handles when "Property" is accessed.  You'll need to produce an AST which represents how to access the member.  A simple way to do this would be if you had a "static bool TryGetProperty(string name, Class1 class, out object res)" method then you could generate an expression tree which just calls that method and if it succeeds returns the result, otherwise you fallback to the binder that called you.  This would also have the result of making this work w/ C# dynamic or other dynamic languages.

An easier way to do this would be if you could make Class1 inherit from .NET 4's DynamicObject class.  In that case you can just override TryGetMember and return the value (rather than dealing w/ the ASTs and IDMOP protocol).

From: ironpython-users-bounces+dinov=microsoft.com at python.org [mailto:ironpython-users-bounces+dinov=microsoft.com at python.org] On Behalf Of zxpatric
Sent: Thursday, June 16, 2011 9:32 AM
To: ironpython-users at python.org
Subject: [Ironpython-users] (Resend after group email address changed): Way to extend IronPython to application specific request without changing its source code?

Hi,

I am looking to extend IronPython in the way that a script like following can run:

         Class1 c1 = Class1()
         print c1.Property

with Class1 defined in .NET module:

public class Class1
{
private int a;
public int A
{
    get { return a;}
    set { a=value;}
}
};

Property is not a .NET property of Class1 but there is an applicatin hard "rule" for example that "Class1.Property" is equal to:

public int Property
{
get { return A+1;}
}

How may I append this rule (as a .NET module to IronPython or Microsoft.Scripting.dll?) without touching the IronPython source so that print c1.Property could be correctly inteperated by IronPython?

Thanks
-Patrick.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20110616/23bf6bb8/attachment-0001.html>


More information about the Ironpython-users mailing list