简体   繁体   中英

Raising explicit implemented event

Consider the following constellation.

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>());
    }
}

I have a Interface and a Generic Interface which extends the first Interface and a class that extends the generic Interface. The generic interface hides the not generic one, via the new keyword. The Update method should raise both, the not-generic and the generic one. And that is the problem I am dealing with at the moment. The resulting error is:

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

But I am in Foo, or do I miss something?

So, what I want is, regardless on which event the client has been registerd, it should be notified. I also should mention, that both, add and remove must work, so there is no option with delegates or something like that.

Just use the private handler variable, this should do the trick.

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

It would probably also be a good idea to check BarEvent and handler for null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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