我想从C#创建一个IronPython类的实例,但我当前的尝试似乎都失败了。在
这是我当前的代码:
ConstructorInfo[] ci = type.GetConstructors();
foreach (ConstructorInfo t in from t in ci
where t.GetParameters().Length == 1
select t)
{
PythonType pytype = DynamicHelpers.GetPythonTypeFromType(type);
object[] consparams = new object[1];
consparams[0] = pytype;
_objects[type] = t.Invoke(consparams);
pytype.__init__(_objects[type]);
break;
}
我可以通过调用t.Invoke(consparams)获得对象的创建实例,但是__init__
方法似乎没有被调用,因此我从Python脚本设置的所有属性都没有被使用。即使使用显式的pytype.__init__
调用,构造的对象似乎仍然没有初始化。在
使用ScriptEngine.Operations.CreateInstance似乎也不管用。在
我使用.NET4.0和IronPython2.6 for.NET4.0。在
编辑:关于我打算如何做的小说明:
在C中,我有一个类,如下所示:
^{pr2}$在Python中,以下代码:
class MyClass(object):
def __init__(self):
print "this should be called"
Foo.Instantiate(MyClass)
似乎从未调用__init__
方法。在
我想我用.NET
Type
类解决了我自己的问题,似乎已经放弃了Python类型信息。在用
IronPython.Runtime.Types.PythonType
替换它效果很好。在这段代码适用于IronPython2.6.1
根据澄清问题编辑
^{pr2}$看起来您正在寻找给this SO question的答案。在
相关问题 更多 >
编程相关推荐