StyleCop安装及配置

How to modify the default new class template for C# in Visual Studio 2008 or 2010?

可参考博客:

http://www.rhyous.com/2010/02/17/how-to-modify-the-default-new-class-template-in-visual-studio-2008/

http://www.thecodinghumanist.com/Content/HowToEditVSTemplates.aspx

1.StyleCop安装网址:http://stylecop.codeplex.com/可下载最新版本

实现前系统默认方式:

?

1

2

3

4

5

6

7

8

9

10

11

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace MyNameSpace

{

    class MyClass

    {

    }

}

  

需要实现后的方式:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace MyNameSpace

{

    public class MyClass

    {

        #region Member Variables

        #endregion

        #region Constructors

        /// <summary>

        /// The default Constructor.

        /// </summary>

        public MyClass()

        {

        }

        #endregion

        #region Properties

        #endregion

        #region Functions

        #endregion

        #region Enums

        #endregion

    }

}

操作步骤:

1、找到Class.zip文件包。具体目录因操作系统不同而不同,如下:
Visual Studio 2008

· For 64 bit: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

· For 32 bit: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Visual Studio 2010

· For 64 bit: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

· For 32 bit: C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

2、解压Calss.zip文件包找到并打开 Class.cs 文件.

文件内容如下::

?

1

2

3

4

5

6

7

8

9

10

11

using System;

using System.Collections.Generic;

$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;

$endif$using System.Text;

namespace $rootnamespace$

{

    class $safeitemrootname$

    {

    }

}

3、将文件格式写成如下方式:

 //-----------------------------------------------------------------------
// <copyright file="$safeitemrootname$.cs" company="Company">
// Copyright (C) Company. All Rights Reserved.
// </copyright>
// <author></author>
// <summary></summary>
//-----------------------------------------------------------------------

namespace $rootnamespace$
{
 using System;
 using System.Collections.Generic;
 $if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
 $endif$using System.Text;

 /// <summary>
 /// 
 /// </summary>
 public class $safeitemrootname$
 {
  #region Fields
  #endregion
  #region Constructors
  /// <summary>
  /// Initializes a new instance of the $safeitemrootname$ class.
  /// </summary>
  public $safeitemrootname$()
  {
  }
  #endregion
  #region Finalizers (Destructors)
  #endregion
  #region Delegates
  #endregion
  #region Events
  #endregion
  #region Enums
  #endregion
  #region Interfaces
  #endregion
  #region Properties
  #endregion
  #region Indexers
  #endregion
  #region Methods
  #endregion
  #region Structs
  #endregion
  #region Classes
  #endregion
 }
}

保存该文件并重新打包成Class.zip。(该文件已经默认写好全部集成在了Vss中的Class.zip内只需要覆盖

Rebuild the zip file with the new Class.cs. Be careful to build the zip file correctly.

4、找到如下目录进行覆盖:
Visual Studio 2008

· For 64 bit: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

· For 32 bit: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Visual Studio 2010

· For 64 bit:

·  C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

· C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\5052\Class.zip

· 

· For 32 bit:

·  C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

· C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\5052\Class.zip

· 

5、以管理员身份打开“命令提示符”,执行如下命令

1. Visual Studio 2008
64-bit

cd C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\

32-bit

cd C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\

Visual Studio 2010
64-bit

cd C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\

32-bit

cd C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\

2. Run this command:

devenv.exe /installvstemplates

6、集成到VS2010

a.关闭工程,打开工程的.csproj文件准备编辑;

b.找到  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 这一行;

c.在该行下面加上 <Import Project="$(ProgramFiles)\MSBuild\StyleCop\v4.7\StyleCop.targets" />

d.保存文件。

7出现CA0503CA0505警告提示如何解决

集成到vs2010后会经常出现,CA0503,CA0505警告提示,该提示可以通过如下方式进行禁用。

A、通过注册表,运行regedit打开注册表找到下面节点:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0\Setup\EDev\CodeAnalysisErrorListViolationLimit

系统默认是最多200代码分析警告显示错误列表。查看更多警告,你必须修改或增加这些警告以提高性能。你通过改变注册表键中的这个值从而超过这个限度,,如把200改成1000,注意不能无限放大。

B、如果上述方法还是没能够解决请打开项目中的.csproj文件,查找下是否在删除<PropertyGroup>节点中含有CodeAnalysisRules/CodeAnalysisRuleSet 重新的生成下项目即可

转载于:https://www.cnblogs.com/tomfang/p/3209423.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
StyleCop的终极目标是让所有人都能写出优雅和一致的代码,因此这些代码具有很高的可读性。 早就听说了微软内部的静态代码检查和代码强制格式美化工具 StyleCop StyleCop不是代码格式化(代码美化)工具,而是代码规范检查工具(Code Review 工具),它不仅仅检查代码格式,而是编码规范,包括命名和注释等。 StyleCop目的是帮助项目团队执行一系列常用的源代码格式规范,这些规范是关于如何开发布局规整,易读,易维护并且文档良好的优雅代码的 StyleCop 现在包含了 200 个左右的最佳实践规则(best practice rules),这些规则与 Visual Studio 2005 和 Visual Studio 2008 中默认的代码格式化规则是一致的。 StyleCop可以作为 Visual studio 的插件运行,在VS中对一个C#文件点击右键后的: 选择“Run StyleCop” 在VS执行后,执行效果如下: StyleCop也可以作为 MSBuild 任务(安装时有选项)通过命令行执行。 StyleCop只是一个代码格式的检查工具,它会根据预定义的C#代码格式的最佳实践,对我们的源代码进行检查,并给出不符合编码风格的错误提示。这一点来说与微软的另一个代码检查工具 FxCop 很相似,但 FxCop 是对 dll (compiled binaries) 进行检查,所以 FxCop 适用于新项目通过持续集成工具来使用的情况。也就是说 FxCop 是项目级别的,而 StyleCop是代码级别的,更适合于程序员在编程过程中使用。 与 FxCop 的另一个不同是,SourceAnalysis StyleCop不提供灵活的规则设置,而是使用所谓 one-size-fits-all 的方式强制人们用同样的习惯书写代码,因此 SourceAnalysis (StyleCop)的终极目标是:The ultimate goal of Source Analysis is to allow you to produce elegant, consistent code that your team members and others who view your code will find highly readable. StyleCop检查的规则包括: 布局(Layout of elements, statements, expressions, and query clauses ) 括号位置(Placement of curly brackets, parenthesis, square brackets, etc ) 空格(Spacing around keywords and operator symbols ) 行距(Line spacing ) 参数位置(Placement of method parameters within method declarations or method calls ) 元素标准排列(Standard ordering of elements within a class ) 注释格式(Formatting of documentation within element headers and file headers ) 命名(Naming of elements, fields and variables ) 内置类型的使用(Use of the built-in types ) 访问修饰符的使用(Use of access modifiers ) 文件内容(Allowed contents of files ) Debugging文本(Debugging text)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值