• 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设置规则,这里可以看到自己新添的规则。

  • 相关阅读:
    js 数据结构-栈与队列
    mysql (已解决)Access denied for user 'root'@'localhost' (using password: NO)
    mysql (已解决p)MYSQL5.7启动不了,本地计算机上的 MySQL57 服务启动后停止。
    mysql 慢查询日志,灾难日志恢复,错误日志
    php json的相关操作
    (转)LitJson 遍历key
    (转)用Eclipse进行C++开发时Bianry not found的问题解决
    (转) 在Eclipse中进行C/C++开发的配置方法(20140721最新版)
    (转)如何在eclipse的配置文件里指定jdk路径
    (转)mongodb分片
  • 原文地址:https://www.cnblogs.com/xbblogs/p/6641641.html
Copyright © 2020-2023  润新知