如何在C#中处理在类方法中创建的对象

时间:2014-01-09 06:15:06

标签: c#

我有一个类,我在其中编写了几个方法。我在该类中声明了一些对象,并在Dispose块中声明了catch个对象。当我尝试在其上应用Ruleset时,它会给出CA2000的警告消息。我的代码如下

 public int ValidationExcelBal(string excelPath, string objectReferenceExcelPath)
        {
            DataTable dtPointList = new DataTable();
            DataTable dtAlarm = new DataTable();
            DataTable dtObjectReference = new DataTable();
            try
            {
                int objectReferenceColNum = -1;
                int objectReferenceAlarmColNum = -1;


                objGGTAutoBindingToolDal.YomitoriExcelToDataTable(excelPath, ref dtPointList, ref dtAlarm);
                objGGTAutoBindingToolDal.ObjectReferenceExcelToData(objectReferenceExcelPath, ref dtObjectReference);

                #region code to find object reference column number in excel sheet and Alarm sheet


                for (int i = 0; i < dtPointList.Columns.Count; i++)
                {
                    for (int k = 0; k < dtPointList.Rows.Count; k++)
                    {
                        if (k < 4)
                        {
                            string name = dtPointList.Rows[k][i].ToString().Replace("\n", "").Replace(" ", "");
                            if (name == "ObjectReference")
                            {
                                objectReferenceColNum = i;
                                break;
                            }
                        }
                    }
                }

                //code to find colomn number of object reference field in Alarm sheet
                for (int j = 0; j < dtAlarm.Columns.Count; j++)
                {
                    string name = dtAlarm.Rows[0][j].ToString();
                    if (name.Equals("Object Reference"))
                    {
                        objectReferenceAlarmColNum = j;
                        break;
                    }
                }

                #endregion

                if (objectReferenceColNum == -1 || objectReferenceAlarmColNum == -1)
                {
                    return 1;
                }

                //if (Convert.ToString(dtObjectReference.Columns[0]).Contains("Bldg Name") || Convert.ToString(dtObjectReference.Columns[1]).Contains("Graphics Name") || Convert.ToString(dtObjectReference.Columns[2]).Contains("Controller Object Reference"))
                //{

                //    return 2;
                //}

                return 3;
            }
            catch (Exception)
            {
                dtPointList.Dispose();
                dtAlarm.Dispose();
                dtObjectReference.Dispose();
                throw;
            }
        }

我的警告如下

Warning 17  CA2000 : Microsoft.Reliability : In method 'GgtAutoBindingToolBal.ValidationExcelBal(string, string)', call System.IDisposable.Dispose on object 'dtPointList' before all references to it are out of scope.    D:\Project\Pragati Installer\GGTAutoBindingBll\GgtAutoBindingToolBal.cs 26  GgtAutoBindingBll

2 个答案:

答案 0 :(得分:3)

最好的办法是将Dispose()的手动调用替换为using语句(超出范围时将调用Dispose()。)

using (DataTable dtPointList = new DataTable()) {
   ...
}

示例:

using (DataTable dtPointList = new DataTable()) {
    using (DataTable dtAlarm = new DataTable()) {
        using (DataTable dtObjectReference = new DataTable()) {
            // your code here, no need to call Dispose() on any of these objects
        }
    }
}

答案 1 :(得分:0)

您只在catch块中调用dispose。如果try块没有异常怎么办?难道它也不应该被放置在那里吗?尝试一下,看看警告是否仍在那里。正如在其他答案中提到的那样 - 使用是您最好的选择。这样,资源将在使用代码块的范围内使用。

public int ValidationExcelBal(string excelPath, string objectReferenceExcelPath)
{
    using (DataTable dtPointList = new DataTable())
    {
        using (DataTable dtAlarm = new DataTable())
        {
            using (DataTable dtObjectReference = new DataTable())
            {
                try
                {
                    int objectReferenceColNum = -1;
                    int objectReferenceAlarmColNum = -1;


                    objGGTAutoBindingToolDal.YomitoriExcelToDataTable(excelPath, ref dtPointList, ref dtAlarm);
                    objGGTAutoBindingToolDal.ObjectReferenceExcelToData(objectReferenceExcelPath, ref dtObjectReference);

                    #region code to find object reference column number in excel sheet and Alarm sheet


                    for (int i = 0; i < dtPointList.Columns.Count; i++)
                    {
                        for (int k = 0; k < dtPointList.Rows.Count; k++)
                        {
                            if (k < 4)
                            {
                                string name = dtPointList.Rows[k][i].ToString().Replace("\n", "").Replace(" ", "");
                                if (name == "ObjectReference")
                                {
                                    objectReferenceColNum = i;
                                    break;
                                }
                            }
                        }
                    }

                    //code to find colomn number of object reference field in Alarm sheet
                    for (int j = 0; j < dtAlarm.Columns.Count; j++)
                    {
                        string name = dtAlarm.Rows[0][j].ToString();
                        if (name.Equals("Object Reference"))
                        {
                            objectReferenceAlarmColNum = j;
                            break;
                        }
                    }

                    #endregion

                    if (objectReferenceColNum == -1 || objectReferenceAlarmColNum == -1)
                    {
                        return 1;
                    }

                    //if (Convert.ToString(dtObjectReference.Columns[0]).Contains("Bldg Name") || Convert.ToString(dtObjectReference.Columns[1]).Contains("Graphics Name") || Convert.ToString(dtObjectReference.Columns[2]).Contains("Controller Object Reference"))
                    //{

                    //    return 2;
                    //}

                    return 3;
                }
                catch (Exception)
                {
                    dtPointList.Dispose();
                    dtAlarm.Dispose();
                    dtObjectReference.Dispose();
                    throw;
                }
            }
        }



    }
}