代码分析:如何克服CA2000 for TableCell

时间:2012-11-16 09:18:10

标签: c# .net code-analysis

我有三个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;
    }

参考

  1. To Dispose or not to Dispose (CA2000)
  2. 更新代码

    使用块来克服警告

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

    问题

    应用程序“使用”块(如更新的代码中所示)是否存在任何陷阱以克服警告?

1 个答案:

答案 0 :(得分:0)

请阅读What’s wrong with use of “Using” block on Webcontrols?,了解相关信息。另请参阅Why would I need to call dispose on ASP.NET Controls?

有些兴趣点(来自上述帖子):

  1. 我们需要做的是确保添加新控件位于Controls的集合中,以便在页面处理时将其处理。
  2. 控件对象实现IDisposable接口。每个父控件都可以在其所有子节点上调用Dispose
  3. 任何正确编写的对象实现IDisposable并且具有在dispose进程中实际清理的状态数据,如果在处置它之后访问其任何公共/受保护/内部属性或方法,则应抛出ObjectDisposedException。 (假设调用Dispose后无效状态。)如果某些类型实际上没有任何要清理的东西,则会忽略此规则,并且不必担心无效状态。