【问题标题】:How can I make VS2010 insert using statements in the order dictated by StyleCop rules如何按照 StyleCop 规则规定的顺序使用语句插入 VS2010
【发布时间】:2011-02-21 22:45:31
【问题描述】:

相关的默认 StyleCop 规则有:

  1. using 语句放在namespace 中。
  2. 按字母顺序对using 语句进行排序。
  3. 但是……Systemusing 先来(仍在试图弄清楚这是否意味着只是using System;using System[.*];)。

所以,我的用例:

  • 我发现了一个错误并决定我至少需要添加一个可理解的 Assert 以减少对下一个人进行调试的痛苦。所以我开始输入Debug.Assert(,智能感知将其标记为红色。我将鼠标悬停在Debugusing System.Diagnostics;System.Diagnostics.Debug 之间我选择前者。这会在所有其他 using 语句之后插入 using System.Diagnostics;。如果 VS2010 没有帮助我编写由于错误警告而无法构建的代码,那就太好了。

如何让 VS2010 更智能?是否有某种设置,或者这是否需要某种成熟的插件?

【问题讨论】:

  • 请记住,即使是 MS 也指出您不必遵守所有规则,而他们自己也不需要(回复:#1)。
  • 嗯......那么他们自己不使用哪些规则?是根据项目决定的吗?

标签: c# visual-studio-2010 intellisense stylecop


【解决方案1】:

您可以使用成熟的插件 Resharper (www.jetbrains.com) 使 VS2010 更智能。它可以为您做所有这些事情(以及更多),而且物有所值。 Resharper 插件“StyleCop for Resharper”甚至可以即时检查 StyleCop 违规情况,并像 Visual Studio 处理错误一样为您的代码添加下划线。

【讨论】:

    【解决方案2】:

    对于 2008 年,我使用 Power Commands 加载项。它包括一个命令来排序和删除未使用的 using 语句。我将其映射到 Ctrl-O、Ctrl-R。它不是自动的,但非常快。

    2010 也有一个 Power Commands,但我认为排序和排序使用语句的东西现在已经内置了。你只需要为它设置一个快捷方式。

    PS。由于资源开销,我不使用 Resharper。每次我告诉人们它会破坏我的硬盘驱动器并推动内存使用量激增时,他们都会告诉我“尝试最新版本 - 现在好多了”。可以说,它从来没有……不过我确实使用 CodeRush Xpress。

    【讨论】:

    • VS2008引入了Remove And Sort
    【解决方案3】:

    关于您的#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")]
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      相关资源
      最近更新 更多