如何引用C#中的事件 [英] How to reference an event in C#

查看:82
本文介绍了如何引用C#中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类,它有一个公共事件叫做 LengthChanged

I have the following class, which has one public event called LengthChanged:

class Dimension
{
    public int Length
    {
        get
        {
            return this.length;
        }
        set
        {
            if (this.length != value)
            {
                this.length = value;
                this.OnLengthChanged ();
            }
    }

    protected virtual void OnLengthChanged()
    {
        var handler = this.LengthChanged;
        if (handler != null)
        {
            handler (this, System.EventArgs.Empty);
        }
    }

    public event System.EventHandler LengthChanged;

    private int length;
}

我想能够注册/注销这个事件的处理程序方法称为 Observer ,它们不了解 Dimension 类的任何内容。我想出了两个场景,其中没有一个是非常令人满意的:

I would like to be able to register/unregister handlers for this event in a method called Observer, which does not know anything about the Dimension class. I have come up with two scenarios, none of which are really satisfying:


  1. 定义界面 ILengthChanged LengthChanged 事件,然后确保维度实现 ILengthChanged 。那么我必须为我定义的每个接口提供 Observer 方法的一个实现。这绝对不够通用。我真的希望能够简单地传递一个 System.EventHandler 事件的引用。

  1. Define an interface ILengthChanged with the LengthChanged event, then make sure Dimension implements ILengthChanged. Then I have to provide one implementation of the Observer method for every interface I define. This by no way generic enough. I'd really want to be able to simply pass in a reference to a System.EventHandler event.

使用 System.Action< System.EventHandler> 回调注册和注销 Observer 方法中的事件处理程序,就像这样:

Use System.Action<System.EventHandler> callbacks for registering and unregistering the event handler in the Observer method, just like that:

class Foo
{
public void Observer(System.Action register,
System.Action unregister)
{
注册(this.MyEventHandler);

class Foo { public void Observer(System.Action register, System.Action unregister) { register (this.MyEventHandler);

    // keep track of the unregister callback, so that we can unregister
    // our event handler later on, if needed...
}

private void MyEventHandler(object sender, System.EventArgs e)
{
    ...
}

}

然后将这样调用:

Foo foo = ...;
Dimension dim = ...;

foo.Observer (x => dim.LengthChanged += x, x => dim.LengthChanged -= x);

当执行时,确实会最终连接 LengthChanged 事件与内部事件处理程序 MyEventHandler 。但这不是很优雅。我会喜欢能够写这个:

and which, when executed, will indeed end up wiring the LengthChanged event with the internal event handler MyEventHandler. But this is not very elegant. I would have loved to be able to write this instead:

Foo foo = ...;
Dimension dim = ...;

foo.Observer (dim.LengthChanged);

但我不知道如何实现。也许我错过了一些非常明显的东西?我想,一些动态魔术可以做这个伎俩,不知何故,但这不会执行编译时类型检查:我不希望 Observer 传递对不满足 System.EventHandler 事件签名的事件的引用。

but I've no idea how this could be achieved. Maybe I am missing something really obvious here? I guess that some dynamic magic could do the trick, somehow, but this would not enforce compile-time type checking: I don't want the users of Observer to pass in references to events which do not satisfy the System.EventHandler event signature.

推荐答案

不幸的是,没有一个这样做的方法。事件一般不是.NET的一流公民 - 尽管F#试图在那里推广它们。

Unfortunately there isn't really a way of doing this. Events aren't first class citizens in .NET in general - although F# tries to promote them there.

传递订阅/取消订阅委托或使用指示活动名称(后者通常较短,但在编译时显然较不安全)。

Either pass in the subscribe/unsubscribe delegate or using a string indicating the name of the event. (The latter is often shorter, but obviously less safe at compile-time.)

这些是反向扩展需要 - 如果有一个更干净的方法,我确定他们会使用它:(

Those are the approaches which Reactive Extensions takes - if there were a cleaner way of doing it, I'm sure they would be using that :(

这篇关于如何引用C#中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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