如何让 VS2010 按照 StyleCop 规则规定的顺序插入 using 语句

发布于 2024-09-02 03:42:04 字数 696 浏览 6 评论 0原文

相关的默认 StyleCop 规则是:

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

所以,我的用例:

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

我怎样才能使 VS2010 更智能?是否有某种设置,或者这是否需要某种成熟的插件?

The related default StyleCop rules are:

  1. Place using statements inside namespace.
  2. Sort using statements alphabetically.
  3. But ... System using come first (still trying to figure out if that means just using System; or using System[.*];).

So, my use case:

  • I find a bug and decide that I need to at least add an intelligible Assert to make debugging less painful for the next guy. So I start typing Debug.Assert( and intellisense marks it in Red. I hover mouse over Debug and between using System.Diagnostics; and System.Diagnostics.Debug I choose the former. This inserts using System.Diagnostics; after all other using statements. It would be nice if VS2010 did not assist me in writing code that won't build due to warnings as errors.

How can I make VS2010 smarter? Is there some sort of setting, or does this require a full-fledged add-in of some sort?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娜些时光,永不杰束 2024-09-09 03:42:04

关于#1,您可以按照此处的说明编辑项目模板项或此处。我已经为 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
lt;/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
lt;/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 的目的是他们的工具自动生成的代码应该满足 StyleCop/FxCop 及其编码指南文档。

With regards to your #1, you can edit the project template items by using the instructions here or here. I've done this for VS 2K8 to make StyleCop and FxCop happy by default, but I haven't gotten around to doing it for 2010 as I find the procedure a bit tedious and there's always a likelihood that a VS service pack could overwrite them.

For instance, I edited the program.cs in the ConsoleApplication template to look like this:

// <copyright file="Program.cs" company="$registeredorganization$">
// Copyright (c) $year$ All Rights Reserved
// </copyright>
// <author></author>
// <email></email>
// <date>$time
lt;/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)
        {
        }
    }
}

and the assemblyinfo.cs to look like this:

// <copyright file="AssemblyInfo.cs" company="$registeredorganization$">
// Copyright (c) $year$ All Rights Reserved
// </copyright>
// <author></author>
// <email></email>
// <date>$time
lt;/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")]

I've submitted an incident at Microsoft Connect to the effect that their tools' auto-generated code should satisfy StyleCop/FxCop and their coding guidelines documents.

穿透光 2024-09-09 03:42:04

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

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

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

For 2008, I use the Power Commands add-in. It includes a command to sort and remove unused using statements. I map that to Ctrl-O, Ctrl-R. It's not automatic, but it's very quick.

2010 has a Power Commands too, but I think the sort and order using statements stuff is now built in. You just need to set up a shortcut for it.

PS. I do not use Resharper because of the resource overhead. Every time I tell people that it thrashes my hard drive and drives memory usage through the roof, they tell me to "try the latest version - it's much better now". Suffice to say, it never has been... I do use CodeRush Xpress though.

人生百味 2024-09-09 03:42:04

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

You can make VS2010 smarter by using Resharper (www.jetbrains.com), a full-fledged add-in. It can do all of these things for you (and very much more), and is well worth the price. The Resharper add-in "StyleCop for Resharper" can even check StyleCop violations on-the-fly and underline your code the same way Visual Studio does for errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文