我protected override void OnFormClosing(FormClosingEventArgs e)
在一些代码中提供了代码,并且代码分析给出了CA1062,因为我不检查是否e
为null。
约定是EventArgs
永远不应该为空;这就是我们拥有的原因EventArgs.Empty
。当然,我可能很愚蠢并且可以通过null
而不是EventArgs.Empty
在引发某些事件时通过,但是这里将是一些自动生成的代码将引发FormClosing事件,因此我只是取消了警告。
是否有一些极端情况可能导致EventArgs由框架为null而不是由程序员引起的?
简短的回答:是的,您可以这样做:
public void DoSomething()
{
OnFormClosing(null);
}
但是,除非您实际执行此类操作,否则可以忽略该警告。
查看Form类的源代码,我们可以找到此方法,该方法将恢复为:
/// <devdoc>
/// <para>Raises the FormClosing event for this form when Application.Exit is called.
/// Returns e.Cancel returned by the event handler.</para>
/// </devdoc>
internal bool RaiseFormClosingOnAppExit() {
FormClosingEventArgs e = new FormClosingEventArgs(CloseReason.ApplicationExitCall, false);
OnFormClosing(e);
return e.Cancel;
}
所以不,e
当WinForms引发事件时,将不会有任何方法可以将null设为null。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句