![](/img/trans.png)
[英]How to detect a bad way of re-throwing a C# Exception using StyleCop or VS2010?
[英]How can I make VS2010 insert using statements in the order dictated by StyleCop rules
相关的默认StyleCop规则是:
using
内声明namespace
。 using
报表按字母顺序。 System
using
率先(仍然试图弄清楚这是否意味着只using System;
或using System[.*];
)。 所以,我的用例:
Debug.Assert(
并且intellisense将其标记为红色。我将鼠标悬停在Debug
之间以及using System.Diagnostics;
和System.Diagnostics.Debug
我选择前者。这using System.Diagnostics;
插入using System.Diagnostics;
在所有其他using
语句之后如果VS2010没有帮助我编写由于警告错误而无法构建的代码,那将是很好的。 如何让VS2010更智能? 是否有某种设置,或者这需要某种完整的加载项?
关于您的#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$</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上提交了一个事件 ,即他们的工具自动生成的代码应该满足StyleCop / FxCop及其编码指南文档。
对于2008年,我使用Power Commands加载项。 它包含一个用于排序和删除未使用的using语句的命令。 我将其映射到Ctrl-O,Ctrl-R。 这不是自动的,但它很快。
2010也有一个Power Commands,但我认为现在内置了使用语句的排序和顺序。你只需要为它设置一个快捷方式。
PS。 由于资源开销,我不使用Resharper。 每次我告诉人们它会摧毁我的硬盘驱动器并通过屋顶驱动内存使用时,他们告诉我“尝试最新版本 - 它现在好多了”。 可以说,它从来没有......我确实使用CodeRush Xpress。
您可以使用成熟的插件Resharper(www.jetbrains.com)使VS2010变得更加智能。 它可以为你做(以及更多)所有这些事情,并且非常物有所值。 Resharper插件“StyleCop for Resharper”甚至可以即时检查StyleCop违规并以与Visual Studio错误相同的方式为您的代码加下划线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.