We have a feature in v1.x which is almost exactly what you want called 
CreateLambda / CreateMethod.  This allows you to give the body of a function 
which takes parameters and it gives you a strongly typed delegate back.  
Unfortunately we removed it from v2.0 because it was getting in the way of our 
refactoring work but that doesn’t mean it won’t come back at some point.

That being said we actually have enough features in place to make this work 
manually.  Here’s the code to do this in Python:

import clr
clr.AddReference('Microsoft.Scripting')
clr.AddReference('IronPython')
from IronPython.Hosting import PythonEngine
from Microsoft.Scripting.Utils import Function
pe = PythonEngine.CurrentEngine

a = pe.EvaluateAs[Function[object, object, bool]]('lambda x,y: x>y')
a(2,3)
a(3,2)

in C# it’d be something like:

Function<object, object, bool> func = pe.EvaluateAs<Function<object, object, 
bool>>(“lambda x,y: x>y”);
bool res1 = func(2,3);
bool res2 = func(3,2);


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bing Li
Sent: Thursday, August 09, 2007 10:27 PM
To: users@lists.ironpython.com
Subject: [IronPython] How to get the result of callback

Hi,guy.

I want to call python lambda expression in C#. The following is lambda 
expression.

"CheckNumber = x,y:x>y"

So, I want to use C# to execute the expression, I wirte a following function 
that,
The parameter expression is lambada expression, the parameters is a key-value 
list. The key is the name of parameter in lambda expression 'x' and 'y'.
 public object CallPythonLambda(string expression, Hashtable parameters) {
            PythonEngine pe = PythonEngine.CurrentEngine;
            //TODO: not implement.
        }

Can anybody help me to complete the function? By the way, the version of 
IronPython is 2.0A3.

Thanks a lot!



_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to