Bis jetzt habe ich eine einfache Klasse, die eine Python-Engine (IronPython) für meine Verwendung umschließt. Obwohl Code groß aussieht, ist es wirklich einfach, also kopiere ich es hier, um klarer mit meinem Problem umzugehen.Führen Sie eine bestimmte Python-Funktion in C# mit IronPython
Hier ist der Code:
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
}
}
Also, es funktioniert gut, aber es erlaubt nur mir auf einmal alle Python-Skript ausführen ... aber was ich möchte, ist zu tun, der Lage sein, bestimmte Funktionen aufzurufen aus einem Pythos-Skript heraus.
Also, meine Frage: Wie rufe ich bestimmte Funktion im geladenen Skript?
Ich habe versucht, einige Informationen oder Tutorials zu finden, aber konnte leider nichts finden.
Haben Sie bei http://stackoverflow.com/questions/13231913/how-do-i-call-a-specific sah -method-from-a-python-script-in-c und http://stackoverflow.com/questions/7053172/how-can-i-call-ironpython-code-from-ac-sharp-app? –
@Simon zuerst ja, zweite nein. Danke für den Link, ich werde es jetzt lesen. – NewProger