StyleCop(C#代碼檢測工具)


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

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

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

  自己定義的dll規則也放在這個目錄下

三、使用方式:打開VS之后選擇一個類或者一個類庫右擊

RunStyleCop運行結果:

四:編寫自己的規則:

1、創建一個類庫,

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

  代碼如下:

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"

2、新建一個和類同名的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

 3、保存並編譯

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

4、使用自己的規則:

  打開VS之后選擇一個類或者一個類庫右擊,選擇 StyleCop Settings設置規則,這里可以看到自己新添的規則。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM