处置或不处置(CA2000)或不

2023-09-02 01:41:57 作者:此籹子╭ァ狠乖

我在一个旧的项目在接通code分析。导致我能理解大部分的言论,但 CA2000:失去范围提前处理的对象是很难正确的。

I'm switching on Code Analysis on an older project. Most remarks that result I can understand, but the CA2000: Dispose objects before losing scope is hard to get right.

例如,从一个ASP.Net页本code:

For instance, this code from an ASP.Net page:

private void BuildTable()
{
    HtmlTableRow tr = new HtmlTableRow();
    HtmlTableCell td = new HtmlTableCell();

    tr.Cells.Add(td);
    // add some controls to 'td'

    theTable.Rows.Insert(0, tr);
    // 'theTable' is an HtmlTable control on the page
}

给出CA消息:

Gives CA messages:

CA2000:Microsoft.Reliability:在方法BuildTable()',调用System.IDisposable.Dispose对象TR的所有引用之前,它超出范围

CA2000 : Microsoft.Reliability : In method 'BuildTable()', call System.IDisposable.Dispose on object 'tr' before all references to it are out of scope.

CA2000:Microsoft.Reliability:在方法BuildTable()',对象'TD'不是沿着所有的异常路径。所有引用之前调用System.IDisposable.Dispose对象'TD'它超出范围。   (约被添加到TD的控制类似的消息。)

CA2000 : Microsoft.Reliability : In method 'BuildTable()', object 'td' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'td' before all references to it are out of scope. (and similar messages about the controls that are added to that 'td'.)

我可以解决第二个问题:

I can resolve the second problem:

private void BuildTable()
{
    HtmlTableRow tr = new HtmlTableRow();
    HtmlTableCell td = new HtmlTableCell();

    try
    {
        tr.Cells.Add(td);
        // add some controls to 'td'

        td = null; // this line is only reached when there were no exceptions
    }
    finally
    {
        // only dispose if there were problems ('exception path')
        if (td != null) td.Dispose();
    }

    theTable.Rows.Insert(0, tr);
}

但我不认为这是可能的解决有关潮流的消息。我无法处置的,因为它仍然需要后,该方法已退出。

But I don't think it is possible to resolve the message about the 'tr'. I can't Dispose of that, because it's still needed after the method has exited.

还是我错过了什么?

顺便说一句:改变这种 theTable.Rows.Insert theTable.Rows.Add 更改CA消息以不是沿着所有的的异常的路径

By the way: changing that theTable.Rows.Insert into theTable.Rows.Add changes the CA message to 'not disposed along all exception paths'

推荐答案

在code分析是无法完全理解你的code,如果你创建一个似乎不设置一次性对象只是警告。你的情况,你应该关掉警告,因为该对象不应该处理才离开的方法。您可以通过自定义code分析规则集或对有此警告的地方很明显,code的分析是错误的每一种方法把警告关闭无论是对整个项目。

The code analysis is unable to completely understand your code and simply warns if you create a disposable object that seems to not be disposed. In your case you should turn off the warning because the object should not be disposed before leaving the method. You can turn warnings off either for the entire project by customizing the code analysis rule set or on each method having this warning where it is obvious that the code analysis is wrong.

这是说,我推荐你使用使用在与的IDisposable 对象处理构建:

That said, I recommend that you use the using construct when dealing with IDisposable objects:

using (var tr = new HtmlTableRow()) {
  using (var td = new HtmlTableCell()) {
    tr.Cells.Add(td);
    theTable.Rows.Insert(0, tr);
  }
}

除了这code是废话,你不希望处理掉你刚才添加到表中的行和单元格。