(IronPython) Execute PythonFunction from C #

Here's the situation ... I want to pass lambda python to a C # method where lambda should be evaluated. when I go into lambda, it turns into an instance of the PythonFunction function. I am stuck here since I don't know how to execute this Python function. I see a “ call ” in this class, but I don’t know where to get the required CodeContext.

Does anyone know how to do this?

+3
source share
3 answers

If you do not want (or cannot) use a delegate, the class ObjectOperationsis the best bet. It is available from engine.ObjectOperations(where engineis your instance ScriptEngine).

if(engine.ObjectOperations.IsCallable(myfunction))
    engine.ObjectOperations.Invoke(myfunction, args);

, (System.Action System.Func, ) . engine.ObjectOperations.ConvertTo , .

+4

:

lambda # IronPython script?

, System.Action, python, , . 100%, , IronPython, System.Action .

, .

+2

, :

var target = myfunction.__code__.Target;
result =  target.DynamicInvoke(new object[] {myfunction, argsForFunction})

myfunction PythonFunction.

0

Source: https://habr.com/ru/post/1727663/


All Articles