c# - 我怎样才能让 VS2010 insert using statements 按照 StyleCop 规则规定的顺序

标签 c# visual-studio-2010 intellisense stylecop

相关的默认 StyleCop 规则是:

  1. using 语句放在 namespace 中。
  2. 按字母顺序对 using 语句进行排序。
  3. 但是 ... System using 排在第一位(仍在尝试弄清楚这是否意味着只是 using System;using系统[.*];).

所以,我的用例:

  • 我发现了一个错误并决定我至少需要添加一个可理解的断言,以减轻下一个人调试的痛苦。所以我开始输入 Debug.Assert( 并且 intellisense 将其标记为红色。我将鼠标悬停在 Debug 上以及 using System.Diagnostics;System.Diagnostics.Debug 我选择前者。这会在所有其他 using 语句之后插入 using System.Diagnostics;。如果 VS2010 这样做就好了不协助我编写因错误警告而无法构建的代码。

如何使 VS2010 更智能?是否有某种设置,或者这是否需要某种完整的插件?

最佳答案

关于您的 #1,您可以使用说明编辑项目模板项 herehere .我已经为 VS 2K8 做了这个,默认情况下让 StyleCop 和 FxCop 开心,但我还没有在 2010 年这样做,因为我发现这个过程有点乏味,而且 VS 服务包总是有可能覆盖它们.

例如,我将 ConsoleApplication 模板中的 program.cs 编辑为如下所示:

// <copyright file="Program.cs" company="$registeredorganization$">
// Copyright (c) $year$ All Rights Reserved
// </copyright>
// <author></author>
// <email></email>
// <date>$time$</date>
// <summary></summary>

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

    /// <summary>
    /// Contains the program's entry point.
    /// </summary>
    internal static class Program
    {
        /// <summary>
        /// The program's entry point.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        private static void Main(string[] args)
        {
        }
    }
}

和 assemblyinfo.cs 看起来像这样:

// <copyright file="AssemblyInfo.cs" company="$registeredorganization$">
// Copyright (c) $year$ All Rights Reserved
// </copyright>
// <author></author>
// <email></email>
// <date>$time$</date>
// <summary></summary>

using System;
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("$projectname$")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("$registeredorganization$")]
[assembly: AssemblyProduct("$projectname$")]
[assembly: AssemblyCopyright("Copyright © $registeredorganization$ $year$")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

[assembly: CLSCompliant(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("$guid1$")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

我已经提交了 an incident在 Microsoft Connect 上表示他们的工具的自动生成代码应该满足 StyleCop/FxCop 和他们的编码指南文档。

关于c# - 我怎样才能让 VS2010 insert using statements 按照 StyleCop 规则规定的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2875279/

相关文章:

c# - 反射排除基类的所有属性和所有其他派生类的特定属性

c# - 分离数百个事件处理程序

.net - 标准 .NET 项目的可维护性指数值应该是多少?

javascript - 如何在 VS Code 中使用 Typescript 定义为我自己的 Javascript 服务获取 Intellisense?

c++ - 从 Intellisense 隐藏 C++ 代码块

c# - 项目资源变量不改变值

c# - 如何从 SOAP 响应 XML 创建 WCF 消息对象

c# - 我什么时候应该注销系统事件以防止内存泄漏?

visual-c++ - 为什么 MSVC 2010 32 位项目链接到 64 位 kernel32.dll?