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 を使用しても機能しないようです。
.NET 4.0 用の IronPython 2.6 で .NET 4.0 を使用しています。
編集:これを行う方法についての小さな説明:
C# では、次のようなクラスがあります。
public static class Foo
{
public static object Instantiate(Type type)
{
// do the instantiation here
}
}
Python では、次のコード:
class MyClass(object):
def __init__(self):
print "this should be called"
Foo.Instantiate(MyClass)
__init__
メソッドが呼び出されることはないようです。