2017-12-24 2 views
0

를 사용하여 C#에서 파이썬 클래스에 메소드로부터 값을 읽어 클래스 : 클래스 모양 (객체) :내가</p> <p>사람이 내가 이것을 달성하기 위해 다음에해야 할 일을 알고 있나요 C#에서이 파이썬 클래스의 면적 법을 읽을 수 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

그러나 어떻게하면 '영역'방법의 값을 읽을 수 있습니까? – user2910705

+0

'class_instance1.area()' – Haytam

관련 문제