5
到目前为止,我有一个简单的类,它包装了一个python引擎(IronPython)供我使用。虽然代码看起来很大,但它非常简单,所以我在这里复制它以更清楚地解决我的问题。在C#中使用IronPython运行特定的Python函数
下面的代码:
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脚本。
所以,我的问题:如何在加载的脚本中调用特定的函数?
我试图找到一些信息或教程,但不幸找不到任何东西。
你有没有看着http://stackoverflow.com/questions/13231913/how-do-i-call-a-specific -method-from-a-python-script-in-c和http://stackoverflow.com/questions/7053172/how-can-i-call-ironpython-code-from-ac-sharp-app? –
@Simon第一个是,第二个没有。感谢您的链接,我现在会阅读它。 – NewProger