代码分析:CA2000警告
本文关键字:CA2000 警告 代码 | 更新日期: 2023-09-27 18:13:11
我有以下带有六个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。可靠性:在支付方法。gvTax_RowDataBound(object, GridViewRowEventArgs)',对象'tableHeaderCell'不沿所有异常路径处置。在对象'tableHeaderCell'的所有引用超出作用域之前,调用System.IDisposable.Dispose。
02)警告2 CA2000: Microsoft。可靠性:在支付方法。gvTax_RowDataBound(object, GridViewRowEventArgs)',对象'tableHeaderCell'不沿所有异常路径处置。在对象'tableHeaderCell'的所有引用都超出作用域之前,调用System.IDisposable.Dispose。
03)警告4 CA2000: Microsoft。可靠性:在支付方法。gvTax_RowDataBound(object, GridViewRowEventArgs)',对象'tableHeaderCell'不沿所有异常路径处置。在对象'tableHeaderCell'的所有引用都超出作用域之前,调用System.IDisposable.Dispose。
04)警告4 CA2000: Microsoft。可靠性:在支付方法。gvTax_RowDataBound(object, GridViewRowEventArgs)',对象'tableHeaderCell'不沿所有异常路径处置。在对象'tableHeaderCell'的所有引用都超出作用域之前,调用System.IDisposable.Dispose。
05)警告5 CA2000: Microsoft。可靠性:采用PayCommission方法。gvCommissionTax_RowDataBound(object, GridViewRowEventArgs)',对象'tableHeaderCell'不沿所有异常路径处置。在对象'tableHeaderCell'的所有引用都超出作用域之前,调用System.IDisposable.Dispose。
06)警告6 CA2000: Microsoft。可靠性:采用PayCommission方法。gvCommissionTax_RowDataBound(object, GridViewRowEventArgs)',对象'tableHeaderCell'不沿所有异常路径处置。在对象'tableHeaderCell'的所有引用都超出作用域之前,调用System.IDisposable.Dispose
发生这种情况是因为代码分析无法跟踪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;
}
现在你的代码实际上变得更干净,因为你减少了重复。
显示警告是因为您正在创建TableHeaderCell的实例。TableHeaderCell实现了IDisposable,而你没有处置它。在这种情况下,警告可能是一个转移注意力的东西,您可以安全地忽略它(右键单击warning -> suppress -> In Source),因为控件应该在处置时处置所有这些东西。