如何创建一个C#事件处理程序可以在IronPython的处理? [英] How do I create a C# event handler that can be handled in IronPython?

查看:94
本文介绍了如何创建一个C#事件处理程序可以在IronPython的处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何创建一个C#事件处理程序可以在IronPython的处理呢?我使用IronPython的2.0.1

How do I create a C# event handler that can be handled in IronPython?

请注意。我能够处理来自系统类的事件,没有任何问题(如Window.KeyDown),但是当我尝试定义我自己的C#事件,当我试图从IronPython的把它挂的异常。

Note that I am using IronPython 2.0.1. I am able to handle events from system classes with no problems (eg Window.KeyDown) but when I try to define my own C# event an exception is raised when I attempt to hook it from IronPython.

引发的异常是ArgumentTypeException,它有消息不能添加到私人事件。该消息似乎很奇怪考虑我想挂钩是公共事件

The exception thrown is ArgumentTypeException and it has the message "cannot add to private event". The message seems odd considering the event I am trying to hook is public.

我的C#类看起来是这样的:

My C# class looks like this:

class Foo
{
    ...

    public event EventHandler Bar;
}



我的IronPython安装程序代码如下:

My IronPython setup code looks like this:

ScriptEngine engine = Python.CreateEngine();
ScriptRuntime runtime = engine.Runtime;
ScriptScope scope = runtime.CreateScope();
ScriptSource source = engine.CreateScriptSourceFromFile("Test.py");
Foo bar = new Foo();
scope.SetVariable("Foo", bar);
source.Execute(scope); <-- Exception is thrown here.



我的IronPython的脚本是这样的:

My IronPython script looks like this:

def BarHandler(*args):
    pass

Foo.Bar += BarHandler

有谁知道,如果我这样做不对吗?

Does anyone know if I am doing this wrong?

或者是IronPython的什么问题?

Or is something wrong with IronPython?

推荐答案

我想通出。

类必须是公众和事件。

例如

public class Foo
{
   ...   

    public event EventHandler Bar;
}

这篇关于如何创建一个C#事件处理程序可以在IronPython的处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆