我有一个 C# 类,看起来有点像:
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 脚本,用于在应用程序中运行,看起来像这样:
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之外),在此赋值之后,将运行默认方法。我知道脚本正在运行,因为脚本的其他部分也在工作。
我该如何做?
OtherMethod
函数的Func<T1, T2>
赋给了问题中定义的ProcessMethod
属性。我认为重要的是两个额外的导入行和对clr.AddReference
的调用,这使我能够使用Func<T1, T2>
。 - Matt Ellen