The common pattern here is not to return any data from the event handler, but to add properties to your event argument object so that the consumer of the event can set the properties which the caller can then access. This is very common in UI handling code; you see the Cancel event concept all over the place.
The following is pseudo code, and not compile ready. Its intent is to show the pattern.
public class MyEventArgs : EventArgs
{
public bool Cancel{get;set;}
}
public bool fireEvent()
{
MyEventArgs e=new MyEventArgs();
//Don't forget a null check, assume this is an event
FireEventHandler(this,e);
return e.Cancel;
}
public HandleFireEvent(object sender, MyEventArgs e)
{
e.Cancel=true;
}
Edit
I like how Jon Skeet worded this: make the EventArgs
mutuable. That is, the consumer of the event can modify the state of the EventArgs
object allowing for the raiser of the event to get to that data.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…