![](/img/trans.png)
[英]How to import static class (or static method) into IronPython (or DLR) using C# code(not python)?
[英]read value from method in a python class in c# using ironpython
我想从C#中的这个python类读取area方法
有人知道我接下来要做的事情:
Python类:类Shape(object):
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();
您必须为此使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.