0

我有以下带有六个 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:在方法“Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)”中,对象“tableHeaderCell”未沿所有异常路径处理。在对对象“tableHeaderCell”的所有引用超出范围之前调用 System.IDisposable.Dispose。

02) 警告 2 CA2000:Microsoft.Reliability:在方法“Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)”中,对象“tableHeaderCell”未沿所有异常路径处理。在对对象“tableHeaderCell”的所有引用超出范围之前调用 System.IDisposable.Dispose。

03) 警告 4 CA2000:Microsoft.Reliability:在方法“Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)”中,对象“tableHeaderCell”未沿所有异常路径处理。在对对象“tableHeaderCell”的所有引用超出范围之前调用 System.IDisposable.Dispose。

04) 警告 4 CA2000:Microsoft.Reliability:在方法“Pay.gvTax_RowDataBound(object, GridViewRowEventArgs)”中,对象“tableHeaderCell”并未沿所有异常路径进行处理。在对对象“tableHeaderCell”的所有引用超出范围之前调用 System.IDisposable.Dispose。

05) 警告 5 CA2000:Microsoft.Reliability:在方法“PayCommission.gvCommissionTax_RowDataBound(object, GridViewRowEventArgs)”中,对象“tableHeaderCell”未沿所有异常路径进行处理。在对对象“tableHeaderCell”的所有引用超出范围之前调用 System.IDisposable.Dispose。

06) 警告 6 CA2000:Microsoft.Reliability:在方法“PayCommission.gvCommissionTax_RowDataBound(object, GridViewRowEventArgs)”中,对象“tableHeaderCell”未沿所有异常路径进行处理。在对对象“tableHeaderCell”的所有引用超出范围之前调用 System.IDisposable.Dispose。

4

2 回答 2

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;
}

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

于 2014-07-15T09:44:38.010 回答
0

显示警告是因为您正在创建 TableHeaderCell 的实例。TableHeaderCell 实现 IDisposable 并且您没有处理它。在这种情况下,警告可能是一条红鲱鱼,您可以放心地忽略它(右键单击警告 -> 抑制 -> 在源代码中),因为控件在处置时应该处置所有这些东西。

于 2014-07-15T09:35:05.560 回答