2013-01-03 7 views
5

これまでのところ、私はPythonエンジン(IronPython)を使用している単純なクラスを持っています。コードは大きく見えますが、それは本当にシンプルなので、ここで私の問題でより明確にするためにコピーします。IronPythonでC#で特定の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スクリプトの中から。

私の質問:ロードされたスクリプトで特定の関数を呼び出すにはどうすればよいですか?

私はいくつかの情報やチュートリアルを見つけようとしていましたが、残念ながら何も見つかりませんでした。

+1

あなたは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? –

+0

@Simon firstはい、second no。リンクをありがとう、私は今それを読むでしょう。 – NewProger

答えて

8

コメントのおかげで、私はそれをどのように使用するかを理解することができました。ここで私は今持っているものだ。それをテストするために

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)); 
関連する問題