将IronPython方法分配给C#委托 [英] Assigning an IronPython method to a C# delegate
问题描述
我有一个C#类,看起来有点像:
I have a C# class that looks a little like:
public class MyClass
{
private Func<IDataCource, object> processMethod = (ds) =>
{
//default method for the class
}
public Func<IDataCource, object> ProcessMethod
{
get{ return processMethod; }
set{ processMethod = value; }
}
/* Other details elided */
}
我有一个IronPython脚本可以在应用程序中运行,看起来像MyApp导入myObj #instance中的
And I have an IronPython script that gets run in the application that looks like
from MyApp import myObj #instance of MyClass
def OtherMethod(ds):
if ds.Data.Length > 0 :
quot = sum(ds.Data.Real)/sum(ds.Data.Imag)
return quot
return 0.0
myObj.ProcessMethod = OtherMethod
但是当 ProcessMethod
被调用IronPython),在这个分配之后,运行默认的方法。
But when ProcessMethod
gets called (outside of IronPython), after this assignment, the default method is run.
我知道脚本的运行是因为脚本的其他部分工作。
I know the script is run because other parts of the script work.
我该怎么做?
推荐答案
我做了一些进一步的Google搜索,找到一个关于IronPython的黑暗角落: http://www.voidspace.org.uk/ironpython/ dark-corners.shtml
I did some further Googling and found a page about the darker corners of IronPython: http://www.voidspace.org.uk/ironpython/dark-corners.shtml
我应该做的是这样的:
from MyApp import myObj #instance of MyClass
import clr
clr.AddReference('System.Core')
from System import Func
def OtherMethod(ds):
if ds.Data.Length > 0 :
quot = sum(ds.Data.Real)/sum(ds.Data.Imag)
return quot
return 0.0
myObj.ProcessMethod = Func[IDataSource, object](OtherMethod)
这篇关于将IronPython方法分配给C#委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!