Help me to implement an event, which handler can cancel it.
public class BuildStartEventArgs : EventArgs { public bool Cancel { get; set; } } class Foo { public event EventHandler<BuildStartEventArgs> BuildStart; private void Bar() { // build started OnBuildStart(new BuildStartEventArgs()); // how to catch cancellation? } private void OnBuildStart(BuildStartEventArgs e) { if (this.BuildStart != null) { this.BuildStart(this, e); } } }
You need to modify this code:
to something like this:
Classes in .NET have reference semantics, so you can see any changes made to the object the parameter of the event references.