Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Events of a non-delegate type

I've implemented a class that looks like this interface:

[ImmutableObject(true)]
public interface ICustomEvent
{
    void Invoke(object sender, EventArgs e);

    ICustomEvent Combine(EventHandler handler);
    ICustomEvent Remove(EventHandler handler);

    ICustomEvent Combine(ICustomEvent other);
    ICustomEvent Remove(ICustomEvent other);
}

This CustomEvent class works much like a MulticastDelegate. It can invoked. It can be combined with another CustomEvent. And a CustomEvent can be removed from another CustomEvent.

Now, I want to declare a class like this:

class EventProvider
{
    public event CustomEvent MyEvent;

    private void OnMyEvent()
    {
        var myEvent = this.MyEvent;
        if (myEvent != null) myEvent.Invoke(this, EventArgs.Empty);
    }
}

Unfortunately, this code does not compile. A Compiler Error CS0066 appears:

'EventProvider.MyEvent': event must be of a delegate type

Basically, what I need is a property that has add and remove accessors instead of get and set. I think the only way to have that is using the event keyword. I know that one obvious alternative is to declare two methods that would do the adding and removing, but I want to avoid that too.

Does anybody knows if there is a nice solution this problem? I wonder if there is any way to cheat the compiler to accept a non-delegate type as an event. A custom attribute, perhaps.

By the way, someone asked a similar question in experts-exchange.com. Since that site is not free, I can't see the responses. Here is the topic: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21697455.html

like image 436
jpbochi Avatar asked Jul 15 '09 21:07

jpbochi


People also ask

Can we use events without delegates?

Yes, you can declare an event without declaring a delegate by using Action.

What will happen if a delegate has a non void return type?

When the return type is not void as above in my case it is int. Methods with Int return types are added to the delegate instance and will be executed as per the addition sequence but the variable that is holding the return type value will have the value return from the method that is executed at the end.

What is the difference between a delegate and an event?

A delegate is a reference type variable that holds the reference to a method. An event is a delegate type class member used by the object or class to provide notification to other objects that an event has occurred.

What are delegates in an event?

Event delegation refers to the process of using event propagation (bubbling) to handle events at a higher level in the DOM than the element on which the event originated. It allows us to attach a single event listener for elements that exist now or in the future.


1 Answers

Try this:

CustomEvent myEvent

public event EventHandler MyEvent {
    add { myEvent = myEvent.Combine(value); }
    remove {myEvent = myEvent.Remove(value); }
}

You can add and remove normal EventHandler delegates to it, and it will execute the add and remove accessors.


EDIT: You can find a weak event implementation here.
2nd EDIT: Or here.

like image 164
SLaks Avatar answered Oct 28 '22 10:10

SLaks