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

Kevin Hazzard, C# MVP wkhazzard at gmail.com
Thu Jun 16 20:55:37 CEST 2011


Inheriting from DynamicObject is the way to go for 90% of applications. I
implement most of my dynamic types that way, as Dino suggested. If you
already have a base class though, implementing IDynamicMetaObjectProvider
isn't all that difficult. But you should have good working knowledge of
Expression Trees before you dive into that.

Kevin

On Thu, Jun 16, 2011 at 1:17 PM, Dino Viehland <dinov at microsoft.com> wrote:

>  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.****
>
> ** **
>
> _______________________________________________
> Ironpython-users mailing list
> Ironpython-users at python.org
> http://mail.python.org/mailman/listinfo/ironpython-users
>
>


-- 
Kevin Hazzard
http://manning.com/hazzard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20110616/b7746314/attachment.html>


More information about the Ironpython-users mailing list