我有三个CA2000警告的代码。对于cellFirst变量,我可以通过使用“using”块来克服这个问题。但对于其他两个标题单元,控件将在辅助函数中创建。
CA2000:Microsoft.Reliability:在方法GetTableCell(string,int,string,string)中,对象'lnkHide'未沿所有异常路径放置。在对所有引用超出范围之前,调用System.IDisposable.Dispose对象'lnkHide'。
CA2000:Microsoft.Reliability:在方法GetTableCell(string,int,string,string)中,对象'ltlText'未沿所有异常路径放置。在对对象'ltlText'的所有引用超出范围之前调用System.IDisposable.Dispose。
CA2000:Microsoft.Reliability:在方法GetTableCell(string,int,string,string)中,对象'newCell'未沿所有异常路径放置。在对对象'newCell'的所有引用都超出范围之前调用System.IDisposable.Dispose。
CODE
protected void grdTransactions_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e != null)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow newHeaderRow = null;
try
{
newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
using (TableHeaderCell cellFirst = new TableHeaderCell())
{
cellFirst.ColumnSpan = 1;
cellFirst.Text = "FIRST";
newHeaderRow.Cells.Add(cellFirst);
}
//Associate
#region Associate
TableHeaderCell cellAssociate = GetTableCell("tableColGroupAssociate", 4, "associateHide", "Associate Transaction Info");
#endregion
//Financial
#region Financial
TableHeaderCell cellFinancial = GetTableCell("tableColGroupTransaction", 5, "financialHide", "Financial Transaction Info");
#endregion
newHeaderRow.Cells.Add(cellAssociate);
newHeaderRow.Cells.Add(cellFinancial);
((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);
}
finally
{
if (newHeaderRow != null)
{
newHeaderRow.Dispose();
newHeaderRow = null;
}
}
}
}
}
private static TableHeaderCell GetTableCell(string cssClassName, int colSpan, string hideClassName, string displayName)
{
TableHeaderCell newCell = new TableHeaderCell();
newCell.ColumnSpan = colSpan;
newCell.CssClass = cssClassName;
LiteralControl ltlText = new LiteralControl();
ltlText.Text = displayName;
newCell.Controls.Add(ltlText);
HyperLink lnkHide = new HyperLink();
lnkHide.Text = SupportToolUIResource.HideLinkText;
lnkHide.CssClass = hideClassName;
lnkHide.Target = SupportToolUIResource.HideLinkTarget;
newCell.Controls.Add(lnkHide);
return newCell;
}
参考:
更新代码
使用块来克服警告
using (TableHeaderCell cellAssociate = new TableHeaderCell())
{
GetTableCell(cellAssociate,"tableColGroupAssociate", 4, "associateHide", "Associate Transaction Info");
newHeaderRow.Cells.Add(cellAssociate);
}
private static void GetTableCell(TableHeaderCell cellAssociate, string cssClassName, int colSpan, string hideClassName, string displayName)
{
cellAssociate.ColumnSpan = colSpan;
cellAssociate.CssClass = cssClassName;
using (LiteralControl ltlText = new LiteralControl())
{
ltlText.Text = displayName;
cellAssociate.Controls.Add(ltlText);
}
using (HyperLink lnkHide = new HyperLink())
{
lnkHide.Text = SupportToolUIResource.HideLinkText;
lnkHide.CssClass = hideClassName;
lnkHide.Target = SupportToolUIResource.HideLinkTarget;
cellAssociate.Controls.Add(lnkHide);
}
}
问题
应用程序“使用”块(如更新的代码中所示)是否存在任何陷阱以克服警告?
答案 0 :(得分:0)
请阅读What’s wrong with use of “Using” block on Webcontrols?,了解相关信息。另请参阅Why would I need to call dispose on ASP.NET Controls?
有些兴趣点(来自上述帖子):