StyleCop(C#代碼檢測工具)

1、StyleCop是微軟的一個開源的靜態代碼分析工具,檢查c#代碼一致性和編碼風格。c#

2、下載地址   http://stylecop.codeplex.com/releases/view/79972ide

  默認安裝目錄:C:\Program Files (x86)\StyleCop 4.7工具

  本身定義的dll規則也放在這個目錄下編碼

3、使用方式:打開VS以後選擇一個類或者一個類庫右擊spa

RunStyleCop運行結果:code

四:編寫本身的規則:orm

一、建立一個類庫,xml

  新建一個MyCustomAnalyzer.cs文件,引用StyleCop.dll和StyleCop.Csharp.dllblog

  代碼以下:ip

using StyleCop;  
using StyleCop.CSharp;  
  
namespace MyCustomRules  
{  
    /// <summary>  
    /// Custom analyzer for demo purposes.  
    /// </summary>  
    [SourceAnalyzer(typeof(CsParser))]  
    public class MyCustomAnalyzer : SourceAnalyzer  
    {  
        /// <summary>  
        /// Extremely simple analyzer for demo purposes.  
        /// </summary>  
        public override void AnalyzeDocument(CodeDocument document)  
        {  
            CsDocument doc = (CsDocument)document;  
  
            // skipping wrong or auto-generated documents  
            if (doc.RootElement == null || doc.RootElement.Generated)  
                return;  
  
            // check all class entries  
            doc.WalkDocument(CheckClasses);  
        }  
  
        /// <summary>  
        /// Checks whether specified element conforms custom rule CR0001.  
        /// </summary>  
        private bool CheckClasses(  
            CsElement element,  
            CsElement parentElement,  
            object context)  
        {  
            // if current element is not a class then continue walking  
            if (element.ElementType != ElementType.Class)  
                return true;  
  
            // check whether class name contains "a" letter  
            Class classElement = (Class)element;  
            if (classElement.Declaration.Name.Contains("a"))  
            {  
                // add violation  
                // (note how custom message arguments could be used)  
                AddViolation(  
                    classElement,  
                    classElement.Location,  
                    "AvoidUsingAInClassNames",  
                    classElement.FriendlyTypeText);  
            }  
  
            // continue walking in order to find all classes in file  
            return true;  
        }  
    }  
  
}  
AddViolation方法中的三個參數"AvoidUsingAInClassNames"是本身定義的規則,這個規則就是下文xml中的 Rule Name="AvoidUsingAInClassNames"

二、新建一個和類同名的xml文件

  MyCustomAnalyzer.xml內容以下:

<?xml version="1.0" encoding="utf-8" ?>  
<SourceAnalyzer Name="My Custom Rule">  
    <Description>  
        Custom rule for demo purposes.  
    </Description>  
    <Rules>  
        <Rule Name="AvoidUsingAInClassNames" CheckId="CR0001">  
            <Context>不能用A字母</Context>  
            <Description>Fires when 'a' letter is used in class name.</Description>  
        </Rule>  
    </Rules>  
</SourceAnalyzer>  

  設置該xml文件屬性:編譯方式爲嵌入式 (即編譯到dll中),Rules中能夠放多個Rule但不要忘了改Name和Id

 三、保存並編譯

  將這個項目生成DLL,把MyCustomAnalyzer.dll放到StyleCop根目錄下。到此自定義規則就完成了。

四、使用本身的規則:

  打開VS以後選擇一個類或者一個類庫右擊,選擇 StyleCop Settings設置規則,這裏能夠看到本身新添的規則。

相關文章
相關標籤/搜索