我有一个类,我在其中编写了几个方法。我在该类中声明了一些对象,并在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
答案 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;
}
}
}
}
}