[英]How to call an IronPython 2 method from C#
我可以將IronPython 2類返回到C#。 IronPython 2在該類上調用成員的新方法是什么?
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);
我以為我要做的就是
var methodResult = PythonOps.Invoke(codeContext, pythonObject, SymbolTable.StringToId(member), args);
但是創建虛擬的CodeContext似乎並不正確。 我覺得我應該能夠從我的
code.Execute();
會運行創建類的Python文件,以及執行產生的作用域。
找到了一種方法:
var ops = engine.Operations;
var x = ops.GetMember(pythonObject, member);
var h = ops.Call(x, new object[0]);
好像Operations產生了一個具有有用成員的OperationsObject。
查看DLR代碼(Microsoft.Scripting.Hosting),但是我發現不贊成使用Call:
[Obsolete("Use Invoke instead")]
public object Call(object obj, params object[] parameters) {
return _ops.Invoke(obj, parameters);
}
我的腳本版本0.9.20209還沒有調用。
更新到較新的IronPython 2.6Beta及其腳本dll之后,我發現我可以編寫:
var h = ops.InvokeMember(pythonObject, member, new object[0]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.