2010-10-18 46 views
5

我有一個C#類,它看起來有點像:分配的IronPython的方法將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 */ 
} 

而且我有一個獲取,看起來像

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 
的應用程序運行的IronPython的腳本

ProcessMethod被調用(在IronPython之外)時,在此賦值之後,將運行默認方法。

我知道腳本運行,因爲腳本的其他部分工作。

我應該怎麼做?

回答

11

我做了一些谷歌搜索還發現一個網頁約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) 
+0

回覆舊的文章,但你會不會還需要從某處導入名稱IDataSource才能使用? – 2017-12-07 01:33:10