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