Wednesday, February 24, 2016

编程规范Coding standard

编程规范文字版:包含Comments, Environment, Functions, General, Java, Names, Tests,详细书籍Clean Code的285-314页。

Tool:
http://stackoverflow.com/questions/38635/what-static-analysis-tools-are-available-for-c

FxCop for c#:
http://www.cnblogs.com/zhuqil/archive/2010/12/07/FxCop.html
http://www.codeproject.com/Articles/30666/Steps-to-Write-Your-Own-Custom-Rule-using-FXCOP

base("clsCheck", "MyRules.connection", typeof(clsCheck).Assembly)
clsCheck: class name
MyRules.connection: (proj name).(xml file name)

Add MyRules DLL to FxCop














Now compile JustForTest to a exe and drag it to FxCop



Project MyRules:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.FxCop.Sdk;



namespace MyRules
{
    class clsCheck : BaseIntrospectionRule
    {
        public clsCheck()
            : base("clsCheck", "MyRules.connection", typeof(clsCheck).Assembly)
        {

        }

        public override ProblemCollection Check(Member member)
        {
            Method method = member as Method;
            bool boolFoundConnectionOpened = false;
            bool boolFoundConnectionClosed = false;
            Instruction objInstr = 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.SqlConnection"))
                    {
                        boolFoundConnectionOpened = true;
                    }
                    if (boolFoundConnectionOpened)
                    {
                        if (objInstr.Value.ToString().Contains("System.Data.Common.DbConnection.Close"))
                        {
                            boolFoundConnectionClosed = true;
                        }
                    }
                }
            }    
             if((boolFoundConnectionOpened)&&(boolFoundConnectionClosed ==false))
            {
               Resolution resolu = GetResolution(new string[] { method.ToString() });
               Problems.Add(new Problem(resolu));
            }
            return Problems;
        }
    }
}


connection.xml
<?xml version="1.0" encoding="utf-8" ?>
<Rules>
<Rule TypeName="clsCheck" Category="Database" CheckId="Shiv001">
<Name>Connection object Should be closed</Name>
<Description> Connection objects should be closed</Description>
<Owner> Shivprasad Koirala</Owner>
<Url>http://www.questpond.com</Url>
<Resolution> Call the connection close method </Resolution>
<Email></Email>
<MessageLevel Certainty="99"> Warning</MessageLevel>
<FixCategories> Breaking </FixCategories>
</Rule>
</Rules>


Project JustForTest:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Data.SqlClient;

namespace JustForTest
{
    class Program
    {
        static void Main(string[] args)
        {

        }

        public static byte[] readFully(Stream input)
        {
            SqlConnection objConnection = new SqlConnection();
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

    }



}


Set rules in VS 2013:
https://msdn.microsoft.com/en-us/library/dd264949.aspx
http://blogs.msdn.com/b/codeanalysis/archive/2010/03/26/how-to-write-custom-static-code-analysis-rules-and-integrate-them-into-visual-studio-2010.aspx



1 comment: