2017-12-24 7 views
0

を使用してC#でPythonのクラスにメソッドから値を読みますクラス: クラス形状(オブジェクト):は、私は</p> <p>誰もが、私はこれを達成するために、次の何をすべきか知っているC#で、このPythonのクラスから面積法を読みたいIronPythonの

def __init__(self,x, y): 

    self.x = x 
    self.y = y 

def area(self): 
    return self.x * self.y 

C#コード:

 ScriptEngine engine1 = Python.CreateEngine(); 
     ScriptSource source1 = engine1.CreateScriptSourceFromFile("C:\\C#Projects\\ExcelAddIn\\ExcelAddIn\\Shape.py"); 
     ScriptScope scope1 = engine1.CreateScope(); 
     source1.Execute(scope1); 

     dynamic class_object1 = scope1.GetVariable("Shape"); 
     dynamic class_instance1 = class_object1(); 

答えて

0

これにはCreateInstanceメソッドを使用する必要があります(こちらはhttps://github.com/jdhardy/ironpython/blob/master/Runtime/Microsoft.Scripting/Hosting/ObjectOperations.cs)。

ScriptEngine engine1 = Python.CreateEngine(); 
ScriptSource source1 = engine1.CreateScriptSourceFromFile("C:\\C#Projects\\ExcelAddIn\\ExcelAddIn\\Shape.py"); 
ScriptScope scope1 = engine1.CreateScope(); 
source1.Execute(scope1); 

var class_object1 = scope1.GetVariable("Shape"); 
var class_instance1 = engine1.Operations.CreateInstance(class_object1, 1, 1); // This should create a shape with x = 1 and y = 1 
+0

Thx、これはうまくいくようです。 – user2910705

+0

しかし、その後私は 'area'メソッドの値を読み取ることができますか? – user2910705

+0

'class_instance1.area()' – Haytam

関連する問題

 関連する問題