How to call IronPython function from C#/F#?

Posted by prosseek on Stack Overflow See other posts from Stack Overflow or by prosseek
Published on 2010-06-14T15:59:52Z Indexed on 2010/06/14 16:02 UTC
Read the original article Hit count: 211

Filed under:
|
|

This is kind of follow up questions of http://stackoverflow.com/questions/2969194/integration-of-c-f-ironpython-and-ironruby

In order to use C/C++ function from Python, SWIG is the easiest solution. The reverse way is also possible with Python C API, for example, if we have a python function as follows

def add(x,y):
    return (x + 10*y)

We can come up with the wrapper in C to use this python as follows.

double Add(double a, double b)
{
    PyObject *X, *Y, *pValue, *pArgs;
    double res;

    pArgs = PyTuple_New(2);
    X = Py_BuildValue("d", a);
    Y = Py_BuildValue("d", b);

    PyTuple_SetItem(pArgs, 0, X);
    PyTuple_SetItem(pArgs, 1, Y);
    pValue = PyEval_CallObject(pFunc, pArgs);
    res = PyFloat_AsDouble(pValue);    

    Py_DECREF(X);
    Py_DECREF(Y);
    Py_DECREF(pArgs);
    return res;
}

How about the IronPython/C# or even F#?

  • How to call the C#/F# function from IronPython? Or, is there any SWIG equivalent tool in IronPython/C#?
  • How to call the IronPython function from C#/F#? I guess I could use "engine.CreateScriptSourceFromString" or similar, but I need to find a way to call IronPython function look like a C#/F# function.

© Stack Overflow or respective owner

Related posts about c#

Related posts about F#