请帮助我实现一个事件,处理程序可以取消它.
public class BuildStartEventArgs : EventArgs { public bool Cancel { get; set; } } class Foo { public event EventHandlerBuildStart; 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); } } }
Richard.. 5
您需要修改此代码:
private void Bar() { // build started OnBuildStart(new BuildStartEventArgs()); // how to catch cancellation? }
这样的事情:
private void Bar() { var e = new BuildStartEventArgs(); OnBuildStart(e); if (!e.Cancel) { // Do build } }
.NET中的类具有引用语义,因此您可以看到对事件参数所做的任何更改引用.
您需要修改此代码:
private void Bar() { // build started OnBuildStart(new BuildStartEventArgs()); // how to catch cancellation? }
这样的事情:
private void Bar() { var e = new BuildStartEventArgs(); OnBuildStart(e); if (!e.Cancel) { // Do build } }
.NET中的类具有引用语义,因此您可以看到对事件参数所做的任何更改引用.