相关疑难解决方法(0)

将IronPython方法分配给C#委托

我有一个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 */
}
Run Code Online (Sandbox Code Playgroud)

我有一个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
Run Code Online (Sandbox Code Playgroud)

但是当ProcessMethod被调用(在IronPython之外)时,在这个赋值之后,运行默认方法.

我知道脚本是运行的,因为脚本的其他部分都有效.

我该怎么做?

c# delegates ironpython variable-assignment

5
推荐指数
1
解决办法
2937
查看次数

python3 pythonnet 通用委托

我在 windows7 上安装了 64 位 CPython 3.4。我使用 pythonnet 包(2.0.0.dev1)。我想实例化操作委托,但它给了我一个错误。

def display(num):
     print("num=", num)

import clr
clr.AddReference("System")
import System

paction=System.Action[System.Int32](display)
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

() ----> 1 paction=System.Action[System.Int32](display) 中的 TypeError Traceback(最近一次调用最后一次) TypeError:不可订阅的对象

我想这就是指定泛型的方式。

我已经检查了文档和这篇文章,但仍然没有看到问题。我还使用了 Overload 方法,但也没有帮助:

paction=System.Action.Overloads[System.Int32](display)
Run Code Online (Sandbox Code Playgroud)

() ----> 1 paction=System.Action.Overloads[System.Int32](display) 中的 TypeError Traceback (最近一次调用最后一次) TypeError: 找不到构造函数签名的匹配项

c# python generics callback python.net

5
推荐指数
1
解决办法
2959
查看次数