How to call IronPython function from C#/F#?
Question: 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, not writing the code in a string, but reading from a file.


Answer: You say 'now writing the code in a string, but reading from a file', so ok,
read the file.

Python from F#:

    
    
    let s = File.ReadAllLines("foo.py")
    let engine = Python.CreateEngine()
    let scriptSource = 
        engine.CreateScriptSourceFromString(s, SourceCodeKind.Statements)
    ...
    

F# from Python:

    
    
    import clr   
    clr.AddReferenceToFile("SomeFsLib.dll") 
    

I just got these from the links in this question. Have not tried it, but,
like, it's straightforward, I think it 'just works'. Dunno what else you are
asking for.

