How to check if DataReader is actually closed using the special FxCop rule?

I wrote a couple of custom rules for FxCop 1.36. I wrote code to find the weather, open DataReader is closed or not. But it does not check which DataReader object calls the method Close(), so I cannot be sure that all open DataReader objects are closed!

2nd: If I am a DataReader in 'if / else', for example

if 1=2
 dr = cmd.ExecuteReader();
else
 dr = cmd2.ExecuteReader();
end if

In this case, it will look for two DataReader objects to close.

I am adding my code for clarity.

public override ProblemCollection Check(Member member)
{
    Method method = member as Method;
    int countCatch =0;
    int countErrLog = 0;
    Instruction objInstr = null;
    if (method != null)
    {
        for (int i = 0; i < method.Instructions.Count; i++)
        {
            objInstr = method.Instructions[i];
            if (objInstr.Value != null)
            {
                if (objInstr.Value.ToString()
                    .Contains("System.Data.SqlClient.SqlDataReader"))
                {
                    countCatch += 1;
                }
                if (countCatch>0)
                {
                    if (objInstr.Value.ToString().Contains(
                        "System.Data.SqlClient.SqlDataReader.Close"))
                    {          
                        countErrLog += 1;
                    }
                }
            }
        }
    }
    if (countErrLog!=countCatch)
    {
        Resolution resolu = 
            GetResolution(new string[] { method.ToString() });
        Problems.Add(new Problem(resolu));
    }
    return Problems;
}
+3
source share
1 answer

FxCop ( ). Microsoft , FxCop VS2010. , FxCop . Microsoft , . Phoenix, Visual Studio 2010 Ultimate ( ). .

0

Source: https://habr.com/ru/post/1736091/


All Articles