tags:

views:

425

answers:

1

I can get an IronPython 2 class back to my C#. What is the new IronPython 2 way of calling a member on that class?

object ironPythonClass = scope.GetVariable("Hamish");
object[] args = new object[0];
object pythonObject = engine.Operations.Call(ironPythonClass, args);

var member = "Home";
// old way IronPython 1
// var methodResult = Ops.Invoke(this.pythonObject, SymbolTable.StringToId(member), args);

I thought all I'd have to do was

var methodResult = PythonOps.Invoke(codeContext, pythonObject, SymbolTable.StringToId(member), args);

but creating a dummy CodeContext doesn't seem to be right. I feel as though I should be able to derive one from my

code.Execute();

that runs the Python file creating the class, plus the scope that arises out of that execution.

+2  A: 

Found a way to do it:

var ops = engine.Operations;
var x = ops.GetMember(pythonObject, member);
var h = ops.Call(x, new object[0]);

Looks like the Operations produces an OperationsObject which has useful members.

Looking at the DLR code (Microsoft.Scripting.Hosting) however I see that Call is deprecated:

[Obsolete("Use Invoke instead")]
public object Call(object obj, params object[] parameters) {
    return _ops.Invoke(obj, parameters);
}

My version of scripting 0.9.20209, doesn't yet have the Invoke however.

After updating to the newer IronPython 2.6Beta and its scripting dlls I find I can write:

var h = ops.InvokeMember(pythonObject, member, new object[0]);
Hamish I E Gunn
Updated to the 2.6 beta with newer scripting dlls - I can now write: var h = ops.InvokeMember(pythonObject, member, new object[0]);
Hamish I E Gunn