使用指令、命名空间和程序集参考 - 所有这些都与 StyleCop 混在一起!
我喜欢遵守 StyleCop 的格式化规则,以使代码美观清晰,但我最近遇到了其中一个警告的问题:
必须放置所有 using 指令 命名空间内部。
我的问题是我有 using 指令、程序集引用(用于模拟文件删除)和一个命名空间来在我的一个测试类中进行杂耍:
using System;
using System.IO;
using Microsoft.Moles.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[assembly: MoledType(typeof(System.IO.File))]
namespace MyNamespace
{
//Some Code
}
上面允许测试正常运行 - 但 StyleCop 抱怨 using 指令不在里面命名空间。
将 using 放在命名空间内会出现“MoledType”无法识别的错误。
将 using 和程序集引用放在命名空间内会出现错误
“程序集”不是有效属性 此声明的位置。有效的 此属性的位置 声明是“类型”。所有属性 该块中的内容将被忽略。
看来我已经尝试了所有可以尝试的布局,但都无济于事 - 要么解决方案无法构建,模拟不起作用,要么 StyleCop 抱怨!
有谁知道如何设置这些以使一切都满意吗?或者在这种情况下我是否必须忽略 StyleCop 警告?
I like to adhere to StyleCop's formatting rules to make code nice and clear, but I've recently had a problem with one of its warnings:
All using directives must be placed
inside of the namespace.
My problem is that I have using directives, an assembly reference (for mocking file deletion), and a namespace to juggle in one of my test classes:
using System;
using System.IO;
using Microsoft.Moles.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[assembly: MoledType(typeof(System.IO.File))]
namespace MyNamespace
{
//Some Code
}
The above allows tests to be run fine - but StyleCop complains about the using directives not being inside the namespace.
Putting the usings inside the namespace gives the error that "MoledType" is not recognised.
Putting both the usings and the assembly reference inside the namespace gives the error
'assembly' is not a valid attribute
location for this declaration. Valid
attribute locations for this
declaration are 'type'. All attributes
in this block will be ignored.
It seems I've tried every layout I can but to no avail - either the solution won't build, the mocking won't work or StyleCop complains!
Does anyone know a way to set these out so that everything's happy? Or am I going to have to ignore the StyleCop warning in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两分钟后解决!
我只需要将“MoledType”的完整路径放入程序集引用中 - 这意味着我可以将其保留在命名空间之外,并在其中使用 using 指令,如下所示:
希望有人会发现这很有用!
Solved two minutes later!
I just needed to put the full path of "MoledType" in the assembly reference - meaning I could leave it outside of the namespace with the using directives inside like so:
Hopefully someone'll find this useful!
典型的模式是将所有程序集级别属性放入 AssemblyInfo.cs 文件中。通常,此文件根本没有任何命名空间元素,并且所有程序集属性都是使用完全限定名称定义的。
The typical pattern would be to put all of your Assembly level attributes within the AssemblyInfo.cs file. Typically this file does not have any namespace element at all and all of the assembly attributes are defined using fully qualified names.
同意 Jason 的观点,您应该将其放在 AssemblyInfo.cs 中(项目 -> 属性)。
但!请小心放入 AssemblyInfo.cs 文件中的内容。假设你想使用:
如果你把它放在AssemblyInfo.cs中,项目的所有内部类将对ClassA可见。这并不总是需要的。
Agreed with Jason, you should put this in the AssemblyInfo.cs instead (Project -> Properties).
But! Be careful with what you put in the AssemblyInfo.cs file. Say you want to use:
If you put this in AssemblyInfo.cs, ALL internal classes of the project will be visible to ClassA. This is not always wanted.