代码分析:CA2000警告

时间:2014-07-15 09:14:28

标签: c# asp.net .net ca2000

我有以下代码,有六个CA2000警告。当我进行代码分析时,我会收到此警告。请让我知道如何克服这个警告以及为什么我会收到此警告。请帮助我如何清除此警告并提前致谢。

if (e.Row.RowType == DataControlRowType.Footer)
            {
                decimal num3 = 0;
                foreach (GridViewRow gridViewRow in this.gvTax.Rows)
                {
                    Label label2 = gridViewRow.FindControl("lbltax") as Label;
                    num3 += Convert.ToDecimal(label2.Text);
                }
                int count = e.Row.Cells.Count;
                for (int i = 0; i <= count - 1; i++)
                {
                    e.Row.Cells[i].Visible = false;
                }



                TableHeaderCell tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = "Total Commission";
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(0, tableHeaderCell);

                tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = Math.Round(num, 2).ToString();
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(1, tableHeaderCell);

                tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = "Net Commission";
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(2, tableHeaderCell);

                tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = Math.Round(num - num3, 2).ToString();
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(3, tableHeaderCell);

                tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = "Total Deduction";
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(4, tableHeaderCell);

                tableHeaderCell = new TableHeaderCell();
                tableHeaderCell.Text = Math.Round(num3, 2).ToString();
                tableHeaderCell.ColumnSpan = 1;
                tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
                tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
                e.Row.Cells.AddAt(5, tableHeaderCell);
            }
        }

警告随之而来。

01)警告1 CA2000:Microsoft.Reliability:在方法&#39; Pay.gvTax_RowDataBound(object,GridViewRowEventArgs)&#39;,object&#39; tableHeaderCell&#39;并未沿所有异常路径放置。调用System.IDisposable.Dispose on object&#39; tableHeaderCell&#39;在所有引用都超出范围之前。

02)警告2 CA2000:Microsoft.Reliability:在方法&#39; Pay.gvTax_RowDataBound(object,GridViewRowEventArgs)&#39;,object&#39; tableHeaderCell&#39;并未沿所有异常路径放置。调用System.IDisposable.Dispose on object&#39; tableHeaderCell&#39;在所有引用都超出范围之前。

03)警告4 CA2000:Microsoft.Reliability:在方法&#39; Pay.gvTax_RowDataBound(object,GridViewRowEventArgs)&#39;,object&#39; tableHeaderCell&#39;并未沿所有异常路径放置。调用System.IDisposable.Dispose on object&#39; tableHeaderCell&#39;在所有引用都超出范围之前。

04)警告4 CA2000:Microsoft.Reliability:在方法&#39; Pay.gvTax_RowDataBound(object,GridViewRowEventArgs)&#39;,object&#39; tableHeaderCell&#39;并未沿所有异常路径放置。调用System.IDisposable.Dispose on object&#39; tableHeaderCell&#39;在所有引用都超出范围之前。

05)警告5 CA2000:Microsoft.Reliability:在方法&#39; PayCommission.gvCommissionTax_RowDataBound(object,GridViewRowEventArgs)&#39;,object&#39; tableHeaderCell&#39;并未沿所有异常路径放置。调用System.IDisposable.Dispose on object&#39; tableHeaderCell&#39;在所有引用都超出范围之前。

06)警告6 CA2000:Microsoft.Reliability:在方法&#39; PayCommission.gvCommissionTax_RowDataBound(object,GridViewRowEventArgs)&#39;,object&#39; tableHeaderCell&#39;并未沿所有异常路径放置。调用System.IDisposable.Dispose on object&#39; tableHeaderCell&#39;在所有引用都超出范围之前。

2 个答案:

答案 0 :(得分:2)

这是因为代码分析无法追踪TabeHeaderCell将在所有路径中被丢弃:

TableHeaderCell tableHeaderCell = new TableHeaderCell();
tableHeaderCell.Text = "Total Commission";
tableHeaderCell.ColumnSpan = 1;
tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
e.Row.Cells.AddAt(0, tableHeaderCell);

如果第1行和第6行之间发生异常,那么它将泄漏实例。这是一个边缘情况,你可以只是压制它,但我发现压制CA2000是一个坏习惯,因为有时它可能非常重要来压制它们 - 你可能隐藏内存泄漏

我解决这个问题的方法是使用以下模式:

TableHeaderCell tableHeaderCell = new TableHeaderCell();
try
{
    tableHeaderCell.Text = "Total Commission";
    tableHeaderCell.ColumnSpan = 1;
    tableHeaderCell.HorizontalAlign = HorizontalAlign.Center;
    tableHeaderCell.VerticalAlign = VerticalAlign.Middle;
    e.Row.Cells.AddAt(0, tableHeaderCell);
}
catch
{
    tableHeaderCell.Dispose();
    throw;
}

这会产生使代码非常冗长的副作用,但是您可以重构代码以使用上面的代码创建表头单元格并传入变量:

private static void TableHeaderCell CreateTableHeaderCell(int columnSpan, string text)
{
    //Same code as above except don't add it to e.
    return tableHeaderCell;
}

现在,您的代码实际上变得更清晰,因为您减少了重复。

答案 1 :(得分:0)

显示警告是因为您正在创建TableHeaderCell的实例。 TableHeaderCell实现了IDisposable,你没有处理它。在这种情况下,警告可能一个红色鲱鱼,您可以放心地忽略它(右键单击警告 - > gt;抑制 - &gt;在源中),因为控件应该处理所有这些事情,当它是地布置。