使用IronPython在c#中运行一个特定的Python函数

本文关键字:一个 函数 Python IronPython 运行 使用 | 更新日期: 2023-09-27 17:50:44

到目前为止,我有一个简单的类来包装一个python引擎(IronPython)供我使用。虽然代码看起来很大,但它真的很简单,所以我把它复制到这里,以更清楚地说明我的问题。

代码如下:

public class PythonInstance
{
    ScriptEngine engine;
    ScriptScope scope;
    ScriptSource source;
    public PythonInstance()
    {
        engine = Python.CreateEngine();
        scope = engine.CreateScope();
    }
    public void LoadCode(string code)
    {
        source = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements);
        source.Compile();
    }
    public void SetVariable(string key, dynamic variable)
    {
        scope.SetVariable(key, variable);
    }
    public void RunCode()
    {
        source.Execute(scope);
    }
    public void CallFunction(string function)
    {
        //?????? no idea what to call here
    }
}

所以,它工作得很好,但它只允许我一次执行所有的python脚本…但是我想做的是能够从pythos脚本中调用特定的函数。

所以,我的问题:我如何在加载的脚本中调用特定的函数?

我试图找到一些信息或教程,但不幸的是找不到任何东西。

使用IronPython在c#中运行一个特定的Python函数

感谢评论中的建议,我能够弄清楚如何使用它。下面是我现在的代码:

public class PythonInstance
{
    private ScriptEngine engine;
    private ScriptScope scope;
    private ScriptSource source;
    private CompiledCode compiled;
    private object pythonClass;
    public PythonInstance(string code, string className = "PyClass")
    {
        //creating engine and stuff
        engine = Python.CreateEngine();
        scope = engine.CreateScope();
        //loading and compiling code
        source = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements);
        compiled = source.Compile();
        //now executing this code (the code should contain a class)
        compiled.Execute(scope);
        //now creating an object that could be used to access the stuff inside a python script
        pythonClass = engine.Operations.Invoke(scope.GetVariable(className));
    }
    public void SetVariable(string variable, dynamic value)
    {
        scope.SetVariable(variable, value);
    }
    public dynamic GetVariable(string variable)
    {
        return scope.GetVariable(variable);
    }
    public void CallMethod(string method, params dynamic[] arguments)
    {
        engine.Operations.InvokeMember(pythonClass, method, arguments);
    }
    public dynamic CallFunction(string method, params dynamic[] arguments)
    {
        return engine.Operations.InvokeMember(pythonClass, method, arguments);
    }
}

测试:

        PythonInstance py = new PythonInstance(@"
class PyClass:
    def __init__(self):
        pass
    def somemethod(self):
        print 'in some method'
    def isodd(self, n):
        return 1 == n % 2
");
        py.CallMethod("somemethod");
        Console.WriteLine(py.CallFunction("isodd", 6));

您可以使用ScriptScope。GetVariable来获取实际的函数,然后你可以像调用c#函数一样调用它。像这样使用它:c#代码:

var var1,var2=...
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
engine.ExecuteFile(@"C:'test.py", scope);
dynamic testFunction = scope.GetVariable("test_func");
var result = testFunction(var1,var2);
Python代码:

def test_func(var1,var2):
    ...do something...
我花了一些时间才弄明白,这很简单。可惜没有好的IronPython文档。希望这对你有帮助