我正在尝试使用python.net在mono / ubuntu上调用C#类。目前,我已经成功地使用一个参数进行了简单的函数调用。现在我想做的是将一个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)
mc.method2(f)
吗? - bosnjak