繁体   English   中英

引发明确的实施事件

[英]Raising explicit implemented event

考虑以下星座。

public delegate void BarHandler(Foo sender, FooEventArgs<Object> args);
public delegate void BarHandler<T>(Foo<T> sender, FooEventArgs<T> args);

public interface Foo
{
    Object Value
    { get; }

    event BarHandler BarEvent;

    void Update();
}

public interface Foo<T> : Foo
{
    new T Value
    { get; }

    new event BarHandler<T> BarEvent;
}

public class Baz<T> : Foo<T>
{
    Object Foo.Value
    { get { return Value; } }

    public T Value
    { get; set; }

    private BarHandler handler;

    event BarHandler Foo.BarEvent
    {
        add{ handler += value; }
        remove{ handler -= value; }
    }

    public event BarHandler<T> BarEvent;

    public void Update()
    {
        BarEvent(this, new FooEventArgs<T>());
        (this as Foo).BarEvent(this, new FooEventArgs<Object>());
    }
}

我有一个接口和一个扩展第一个接口的通用接口,以及一个扩展了通用接口的类。 通用接口通过new关键字隐藏了非通用接口。 Update方法应同时引发非通用方法和通用方法。 这就是我目前正在处理的问题。 产生的错误是:

The event BarEvent can only appear on the left hand side of += or -= when used outside of Foo.

但是我在Foo,还是想念什么?

因此,我想要的是,无论客户端已注册哪个事件,都应得到通知。 我还应该提到,添加和删除都必须起作用,因此委托或类似的东西是没有选择的。

只需使用私有handler变量,就可以解决问题。

public void Update()
{
    BarEvent(this, new FooEventArgs<T>());
    handler(this, new FooEventArgs<Object>());
}

检查BarEventhandler是否为null也是一个好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM