2013-01-03 15 views
5

Do tej pory mam prostą klasę, która otacza silnik python (IronPython) do mojego użytku. Chociaż kod wygląda na duży, to jest naprawdę prosty, więc skopiuję go tutaj, aby był bardziej zrozumiały w moim problemie.Uruchomić konkretną funkcję Pythona w języku C# z IronPythonem

Oto kod:

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 
    } 

} 

Tak, to działa świetnie, ale to tylko pozwala mi wykonywać wszystkie skrypt Pythona na raz ... ale to, co chciałbym zrobić, to aby móc nazwać poszczególne funkcje z poziomu skryptu pythona.

Tak, moje pytanie: Jak wywołać określoną funkcję w załadowanym skrypcie?

Próbowałem znaleźć informacje lub samouczki, ale niestety nic nie mogłem znaleźć.

+1

Czy spojrzał na http://stackoverflow.com/questions/13231913/how-do-i-call-a-specific -method-from-a-python-script-in-c i http://stackoverflow.com/questions/7053172/how-can-i-call-ironpython-code-from-ac-sharp-app? –

+0

@Simon pierwszy tak, drugi nie. Dziękuję za link, przeczytam go teraz. – NewProger

Odpowiedz

8

Dzięki sugestii w komentarzach udało mi się wymyślić, jak z niego korzystać. Oto co mam teraz:

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); 
    } 

} 

Aby przetestować go:

 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)); 
Powiązane problemy