2017-12-24 22 views
0

ich die Gegend Methode aus dieser Python-Klasse in C#lesen Wert von Verfahren in einer Python-Klasse in C# Ironpython mit

Weiß jemand, lesen will, was ich dies als nächstes zu tun habe zu erreichen:

Python Klasse: Klasse Form (object):

def __init__(self,x, y): 

    self.x = x 
    self.y = y 

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

C# Code:

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

Antwort

0

Sie müssen dazu die Methode CreateInstance verwenden (hier verfügbar: 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, das scheint zu funktionieren. – user2910705

+0

Aber wie kann ich danach den Wert der 'Bereich'-Methode auslesen? – user2910705

+0

'class_instance1.area()' – Haytam