CA2000:对象未沿所有异常路径处置

发布于 2024-11-25 18:55:51 字数 756 浏览 2 评论 0原文

虽然之前已经讨论过该主题,但建议的解决方案似乎不起作用。

我的表单应用程序中有一个按钮单击回调方法,它显示一个文件夹选择器对话框:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情仇皆在手 2024-12-02 18:55:51

该警告并不是因为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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文