我正在尝试使用 python 中的 C# 类,在单声道/ubuntu 上使用 python.net。
到目前为止,我设法用一个参数工作进行了一个简单的函数调用。我现在要做的是将 python 回调传递给 C# 函数调用。
我在下面尝试了以下变体,但都没有奏效。有人可以展示如何使其工作吗?
// C# - testlib.cs
class MC {
public double method1(int n) {
Console.WriteLine("Executing method1" );
/* .. */
}
public double method2(Delegate f) {
Console.WriteLine("Executing method2" );
/* ... do f() at some point ... */
/* also tried f.DynamicInvoke() */
Console.WriteLine("Done executing method2" );
}
}
Python 脚本
import testlib, System
mc = testlib.MC()
mc.method1(10) # that works
def f():
print "Executing f"
mc.method2(f)
# does not know of method2 with that signature, fair enough...
# is this the right way to turn it into a callback?
f2 = System.AssemblyLoad(f)
# no error message, but f does not seem to be invoked
mc.method2(f2)