簡體   English   中英

將IronPython方法分配給C#委托

[英]Assigning an IronPython method to a C# delegate

我有一個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之外)時,在此賦值之后,將運行默認方法。

我知道腳本是運行的,因為腳本的其他部分都有效。

我該怎么做?

我做了一些谷歌搜索,發現了一個關於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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM