code分析警告有关处置表单 [英] Code Analysis Warning about Disposing a Form

查看:150
本文介绍了code分析警告有关处置表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对夫妇的静态方法其中一个表格转换为控制(如下图所示)。该分析仪标志两种,指出,CA2000:Microsoft.Reliability:在方法'......',对象形式的所有引用之前没有沿着所有的异常路径调用System.IDisposable.Dispose对象'形式'给它超出范围。类似被判标签页

注:对于那些谁没有企业版和分析器菜单,这看起来很像的FxCop输出

我不清楚是我应该做的。如果失败,将引发异常。哪里是我的机会,叫处置

 类Foo
{
  静态公共表FormAsControl()
  {
    Form表单=新的Foo();

    form.TopLevel = FALSE;
    form.FormBorderStyle = FormBorderStyle.None;
    form.Dock = DockStyle.Fill;
    form.Visible = TRUE;

    返回的形式;
  }

  静态公共TabPage的FormAsTabPage()
  {
    Form表单= Foo.FormAsControl();
    TabPage的标签页=新的TabPage();

    tabPage.Text = form.Text;
    tabPage.Controls.Add(表);

    返回标签页;
  }

  ...
}
 

解决方案

CA2000麻烦,太多的假警告。 FxCop的是不是足够聪明,知道如何控制类的工作。它的Dispose()方法只是做了一些有益的的被创建本机控制窗口。只有这样,才会有可能被处置非托管资源。但是,这不会发生,直到你返回的TabPage被添加到一个的TabControl,进而该控件添加到形式,以及这一形式显示()方法被调用。 code,我们看不到(也的FxCop为此事)。此外,他们实际上的的得到处置,即使有一个例外,当本机窗口被销毁。

您可以燮preSS警告加入的try / catch的方法,这样你就可以在catch块调用Dispose()。但是,这将是一个错误,它只是增加了不必要的code,它在运行时不执行任何有用的。使用[燮pressMessage]属性摆脱了警告。

I have a couple of static methods which convert a Form to a Control (shown below). The analyzer flags both, stating, "CA2000 : Microsoft.Reliability : In method '...', object 'form' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'form' before all references to it are out of scope." Similar is flagged for tabPage.

NOTE: for those who don't have Enterprise Edition and the Analyzer menu, this looks a lot like FxCop output.

I'm not clear on what I should be doing. If new fails, an exception will be thrown. Where is my opportunity to call Dispose?

class Foo
{
  static public Form FormAsControl()
  {
    Form form = new Foo();

    form.TopLevel = false;
    form.FormBorderStyle = FormBorderStyle.None;
    form.Dock = DockStyle.Fill;
    form.Visible = true;

    return form;
  }

  static public TabPage FormAsTabPage()
  {
    Form form = Foo.FormAsControl();
    TabPage tabPage = new TabPage();

    tabPage.Text = form.Text;
    tabPage.Controls.Add(form);

    return tabPage;
  }

  ...
}

解决方案

CA2000 is troublesome, too many false warnings. FxCop isn't smart enough to know how the Control class works. Its Dispose() method only does something useful after the native control window gets created. Only then will there be unmanaged resources that could be disposed. But that won't happen until the TabPage you return gets added to a TabControl and that control in turn is added to a form and the Show() method of that form is called. Code we cannot see (nor FxCop for that matter). Furthermore, they actually do get disposed, even when there's an exception, when the native window gets destroyed.

You could suppress the warning by adding try/catch to the methods so you can call Dispose() in the catch block. But that would be a mistake, it just adds unnecessary code that doesn't do anything useful at runtime. Use the [SuppressMessage] attribute to get rid of the warning.

这篇关于code分析警告有关处置表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆