StyleCop 自定义规则:方法参数和变量
我是 StyleCop 的新手,我需要在我工作的地方实施自己的编码标准。我用的是VS2005,无法调试。现在我们无法升级到 VS2008/2010。
我想知道很多事情:
1)如何识别方法参数?我尝试了以下方法,但不知道该去哪里,SDK 文档并没有真正的帮助。
private bool VisitElement(CsElement element, CsElement parentElement, object context)
{
if (element.ElementType == ElementType.Method)
{
...
2) 我怎样才能知道声明没有跟在赋值之后?例如给出的。
int i; // Wrong, give warning
int i = 0; // True usage
3) 我怎样才能发现文档中不只包含 1 个命名空间或只包含 1 个类,以及如何获取它们的标识符(名称)?
真的:
namespace Hello
{
class P{
}
}
- 错误:
namespace Hi {
class C {
}
class E {
}
}
namespace Ho {
class D {
}
}
4)如何找出函数调用并找出去哪里? (即阻止对特定函数的调用)
I am new to StyleCop, and I need to implement own coding standarts for the place I work. I am using VS2005 and cannot debug it. Upgrading to VS2008/2010 is not an option for us now.
I wonder many things:
1) How can I identify the methods parameters? I tried the below but do not know where to go, SDK documentation is not really helpful.
private bool VisitElement(CsElement element, CsElement parentElement, object context)
{
if (element.ElementType == ElementType.Method)
{
...
2) How can I find out that a declaration do not follow an assignment? Ex.given.
int i; // Wrong, give warning
int i = 0; // True usage
3) How can I find out that a document does not contain only 1 namespace or only 1 class inside it and how can I get their identifiers (names)?
True:
namespace Hello
{
class P{
}
}
-
Wrong:
namespace Hi {
class C {
}
class E {
}
}
namespace Ho {
class D {
}
}
4) How can I find out function calls and find out where to? (i.e. Blocking a call to specific function)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于#1,请查看 Microsoft.StyleCop.CSharp.ReadabilityRules.CheckMethodParameters 方法实现(在 Reflector 中或在 http://stylecop.codeplex.com/SourceControl/changeset/view/64d44becb157#Project%2fSrc%2fAddIns%2fCSharp%2fAnalyzers%2fReadabilityRules.MethodParameters.cs)。
对于#2,类似下面的内容应该可以解决问题:
现有的 SA1402 (FileMayOnlyContainASingleClass) 和 SA1403 (FileMayOnlyContainASingleNamespace) 规则应该处理#3。如果它们不适合您的场景,请指定您希望自定义规则以不同的方式执行哪些操作。
#4 应该是 FxCop 规则,而不是 StyleCop 规则,因为它与源代码样式无关。
For #1, take a look at the Microsoft.StyleCop.CSharp.ReadabilityRules.CheckMethodParameters method implementation (either in Reflector or at http://stylecop.codeplex.com/SourceControl/changeset/view/64d44becb157#Project%2fSrc%2fAddIns%2fCSharp%2fAnalyzers%2fReadabilityRules.MethodParameters.cs).
For #2, something like the following should do the trick:
The existing SA1402 (FileMayOnlyContainASingleClass) and SA1403 (FileMayOnlyContainASingleNamespace) rules should take care of #3. If they don't work for your scenario, please specify what you would like a custom rule to do differently.
#4 should be an FxCop rule, not a StyleCop rule since it has nothing to do with source code style.