I want to create an instance of the IronPython class from C #, but my current attempts do not seem to work.
This is my current code:
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; }
I can get the instantiated object from the call to t.Invoke (consparams), but the __init__ method does not seem to be called, and therefore, all the properties that I set from my Python script are not used. Even with an explicit call to pytype.__init__ constructed object is still not initialized.
Using ScriptEngine.Operations.CreateInstance also does not work.
I am using .NET 4.0 with IronPython 2.6 for .NET 4.0.
EDIT : A little clarification on how I intend to do this:
In C #, I have a class as follows:
public static class Foo { public static object Instantiate(Type type) {
And in Python, the following code:
class MyClass(object): def __init__(self): print "this should be called" Foo.Instantiate(MyClass)
The __init__ method is never called.
source share