Creating Instances of IronPython Classes From C#
Asked Answered
S

3

6

I want to create an instance of an IronPython class from C#, but my current attempts all seem to have failed.

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 am able to get the created instance of the object from calling t.Invoke(consparams), but the __init__ method doesn't seem to be called, and thus all the properties that I set from my Python script aren't used. Even with the explicit pytype.__init__ call, the constructed object still doesn't seem to be initialised.

Using ScriptEngine.Operations.CreateInstance doesn't seem to work, either.

I'm using .NET 4.0 with IronPython 2.6 for .NET 4.0.

EDIT: Small clarification on how I'm intending to do this:

In C#, I have a class as follows:

public static class Foo
{
    public static object Instantiate(Type type)
    {
        // do the instantiation here
    }
}

And in Python, the following code:

class MyClass(object):
    def __init__(self):
        print "this should be called"

Foo.Instantiate(MyClass)

The __init__ method never seems to be called.

Speiss answered 4/8, 2010 at 4:59 Comment(2)
possible duplicate of Instantiating a python class in C#Scorpio
After taking a look at the IronPython source, the _init_ method is defined as public void __init__(object o) { }. Whoops.Speiss
I
10

This code works with IronPython 2.6.1

    static void Main(string[] args)
    {
        const string script = @"
class A(object) :
    def __init__(self) :
        self.a = 100

class B(object) : 
    def __init__(self, a, v) : 
        self.a = a
        self.v = v
    def run(self) :
        return self.a.a + self.v
";

        var engine = Python.CreateEngine();
        var scope = engine.CreateScope();
        engine.Execute(script, scope);

        var typeA = scope.GetVariable("A");
        var typeB = scope.GetVariable("B");
        var a = engine.Operations.CreateInstance(typeA); 
        var b = engine.Operations.CreateInstance(typeB, a, 20);
        Console.WriteLine(b.run()); // 120
    }

EDITED according to clarified question

    class Program
    {
        static void Main(string[] args)
        {
            var engine = Python.CreateEngine();
            var scriptScope = engine.CreateScope();

            var foo = new Foo(engine);

            scriptScope.SetVariable("Foo", foo);
            const string script = @"
class MyClass(object):
    def __init__(self):
        print ""this should be called""

Foo.Create(MyClass)
";
            var v = engine.Execute(script, scriptScope);
        }
    }

public  class Foo
{
    private readonly ScriptEngine engine;

    public Foo(ScriptEngine engine)
    {
        this.engine = engine;
    }

    public  object Create(object t)
    {
        return engine.Operations.CreateInstance(t);
    }
}
Investment answered 4/8, 2010 at 7:9 Comment(2)
The thing is that I'm not using scope.GetVariable -- I'm passing the types directly to C# in the form of the C# Type class, mainly because I need to call the function from all sorts of places and the variable might not necessarily be available in that scope.Speiss
Even though I answered my own question, I still think you deserve to be the accepted answer :)Speiss
S
2

I think I solved my own question -- using the .NET Type class seems to have discarded Python type information.

Replacing it with IronPython.Runtime.Types.PythonType works quite well.

Speiss answered 4/8, 2010 at 7:47 Comment(0)
O
0

Looks like you're looking for the answer given to this SO question.

Oligocene answered 4/8, 2010 at 5:10 Comment(3)
The answers never actually said anything about instantiating the object -- the Calculator class in the example has no constructor method and thus it doesn't really answer my question.Speiss
That SO Q's solution works fine for me, and adding an __init__ method does get it executed (using the .Operations instance as shown there). I don't have a .NET 4.0 to try, but I'd be astonished if they broke things in the 4.0 transition.Oligocene
That example isn't entirely suited for me, since I'm passing the type itself to C# code, rather than a scoped variable, i.e. via a function, which might be why the __init__ method isn't called.Speiss

© 2022 - 2024 — McMap. All rights reserved.