将 IronPython 方法分配给 C# 委托

Assigning an IronPython method to a C# delegate(将 IronPython 方法分配给 C# 委托)
本文介绍了将 IronPython 方法分配给 C# 委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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 脚本,它可以在看起来像这样的应用程序中运行

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.

我应该怎么做?

推荐答案

我做了一些进一步的谷歌搜索,发现了一个关于 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# 委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)