C# 导入语句的位置,让 StyleCop 高兴
我是 StyleCop 的忠实粉丝,它让我的生活变得更轻松。许多其他人想到了好的规则,我很高兴通过启用 StyleCop 来遵循它们。最近,我一直在搞乱编码的 Ui 扩展性,并发现了这篇文章:
http://blogs.msdn.com/b/gautamg/archive/2010/01/05/2-hello-world-extension-for-coded -ui-test.aspx
下面的示例代码让 StyleCop 不满意,因为 using
语句位于命名空间之外。但是,我只能移动两个 System*
包 - 第三个包需要定义程序集属性,并且我不能将 assemble:
扔到命名空间内。
有没有一种干净的方法来重新组织这段代码?
using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UITest.Common;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
// Attribute to denote that this assembly has UITest extensions.
[assembly: UITestExtensionPackageAttribute("HelloWorldPackage",
typeof(UITestHelloWorldPackage.HelloWorldPackage))]
namespace UITestHelloWorldPackage
{
internal class HelloWorldPackage : UITestExtensionPackage
{
public override object GetService(Type serviceType)
{
Trace.WriteLine("Hello, World");
return null;
}
....
I am a big fan of StyleCop, it makes my life easier. A bunch of other people have thought of good rules, and I gladly follow them by enabling StyleCop. Recently I have been messing with Coded Ui extensibility, and came across this article:
http://blogs.msdn.com/b/gautamg/archive/2010/01/05/2-hello-world-extension-for-coded-ui-test.aspx
The sample code below makes StyleCop unhappy because the using
statements are outside of the namespace. However, I can move only the two System*
packages in - the third is needed to define an assembly attribute, and I cannot throw assembly:
inside of a namespace.
Is there a clean way to re-organize this code?
using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UITest.Common;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
// Attribute to denote that this assembly has UITest extensions.
[assembly: UITestExtensionPackageAttribute("HelloWorldPackage",
typeof(UITestHelloWorldPackage.HelloWorldPackage))]
namespace UITestHelloWorldPackage
{
internal class HelloWorldPackage : UITestExtensionPackage
{
public override object GetService(Type serviceType)
{
Trace.WriteLine("Hello, World");
return null;
}
....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恕我直言,将 using 放在命名空间内的规则是无用的,并且使代码难以阅读。
IMHO, the rule to put usings inside the namespace is useless and makes the code hard to read.
您不能将
[ assembly: UITestExtensionPackageAttribute()]
属性移至 Properties\AssemblyInfo.cs 文件吗?我猜您引用的文章具有[assemble:]
属性,只是为了将示例保留在单个代码块中。我同意 Daniel 的观点 - 在命名空间内使用
using
语句会让你的代码更难阅读。我建议将该属性移动到您的 AssemblyInfo.cs 文件中,并将您的使用保留在类文件的顶部。这看起来很标准。
希望这有帮助!
Can't you move your
[assembly: UITestExtensionPackageAttribute()]
attribute to your Properties\AssemblyInfo.cs file? I'm guessing the article you referenced had the[assembly:]
attribute there just to keep the example in a single block of code.I agree with Daniel - having
using
statements inside namespaces makes your code harder to read.I'd recommend moving the attribute to your AssemblyInfo.cs file and keep your usings at the top of your class file. That seems pretty standard.
Hope this helps!