CA2000:对象未沿所有异常路径处置
虽然之前已经讨论过该主题,但建议的解决方案似乎不起作用。
我的表单应用程序中有一个按钮单击回调方法,它显示一个文件夹选择器对话框:
private void ButtonSelectReporterFolderClick(object sender, EventArgs e)
{
using (var dialog = new FolderBrowserDialog()) // causes warning
{
if (dialog.ShowDialog() == DialogResult.OK)
{
this.boxReporterFolderPath.Text = dialog.SelectedPath;
}
}
}
这会产生一个警告:
CA2000 : Microsoft.Reliability :在方法“MainWindow.ButtonSelectReporterFolderClick(object, EventArgs)”中,对象“<>g__initLocal”未沿着所有异常路径进行处置。在对象“<>g__initLocal”的所有引用超出范围之前调用 System.IDisposable.Dispose。
我还尝试使用 try
-finally
阻止甚至调用dialog.Dispose,但没有任何阻止,一切都无济于事 - 警告仍然存在,始终在初始化行。
Although topic has been discussed here before, but the proposed solutions don't seem to work..
I have a button-click-callback method in my form application, that shows a folder picker dialog:
private void ButtonSelectReporterFolderClick(object sender, EventArgs e)
{
using (var dialog = new FolderBrowserDialog()) // causes warning
{
if (dialog.ShowDialog() == DialogResult.OK)
{
this.boxReporterFolderPath.Text = dialog.SelectedPath;
}
}
}
This produces a warning:
CA2000: Microsoft.Reliability : In method 'MainWindow.ButtonSelectReporterFolderClick(object, EventArgs)', object '<>g__initLocal' is not disposed along all exception paths. Call System.IDisposable.Dispose on object '<>g__initLocal' before all references to it are out of scope.
I also tried using a try
-finally
block or even calling dialog.Dispose without any blocks, all to no avail - the warning remains, always at the line of initialization.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该警告并不是因为FolderBrowserDialog 未处置,而是因为它有一些实现IDisposable 接口的公共成员,并且您没有单独处置它们。当然,FolderBrowserDialog 对象知道如何处理它的依赖项,但 FxCop 无法知道,因此它会发出警告。只需忽略您案例中的警告即可。
The warning is not because FolderBrowserDialog is not disposed, it is because it has some public members that implements IDisposable interface and you're not disposing them separately. Of course, FolderBrowserDialog object knows how to dispose of it's dependencies but FxCop has no way of knowing that so it gives a warning. Just ignore the warning in your case.