Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding IronPython in C#

Tags:

c#

ironpython

I am just looking into using IronPython with C# and cannot seem to find any great documentation for what I need. Basically I am trying to call methods from a .py file into a C# program.

I have the following which opens the module:

var ipy = Python.CreateRuntime();
var test = ipy.UseFile("C:\\Users\\ktrg317\\Desktop\\Test.py");

But, I am unsure from here how to get access to the method inside there. The example I have seen uses the dynamic keyword, however, at work I am only on C# 3.0.

Thanks.

like image 280
Darren Young Avatar asked May 12 '11 13:05

Darren Young


1 Answers

See embedding on the Voidspace site.

An example there, The IronPython Calculator and the Evaluator works over a simple python expression evaluator called from a C# program.

public string calculate(string input)
{
    try
    {
        ScriptSource source =
            engine.CreateScriptSourceFromString(input,
                SourceCodeKind.Expression);

        object result = source.Execute(scope);
        return result.ToString();
    }
    catch (Exception ex)
    {
        return "Error";
    }
}
like image 57
gimel Avatar answered Oct 04 '22 03:10

gimel