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
EDITED at 2009.07.16 13:01 UTC: ICustomEvent changed