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
If you want to be able to add and remove
CustomEventobjects from the event (instead of regular delegates), there are two options:Make an implicit cast from ICustomEvent to EventHandler (or some other delegate) that returns an instance method of ICustomEvent (probably Invoke), then use the Target property of the delegate to get the original ICustomEvent in the
addandremoveaccessors.EDIT: Like this:
Note that you’ll still need to figure out how to add the first handler if it’s a delegate (if the
myEventfield isnull)Make a writable property of type CustomEvent, then overload the
+and-operators to allow+=and-=on the property.EDIT: To prevent your callers from overwriting the event, you could expose the previous value in CustomEvent (I’m assuming it works like an immutable stack) and, in the setter, add
Note that when the last handler is removed, both
valueandmyEvent.Previouswill benull.