StyleCop 自定义规则:方法参数和变量

发布于 2024-11-17 16:05:17 字数 811 浏览 3 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(1

━╋う一瞬間旳綻放 2024-11-24 16:05:17

对于#1,请查看 Microsoft.StyleCop.CSharp.ReadabilityRules.CheckMethodParameters 方法实现(在 Reflector 中或在 http://stylecop.codeplex.com/SourceControl/changeset/view/64d44becb157#Project%2fSrc%2fAddIns%2fCSharp%2fAnalyzers%2fReadabilityRules.MethodParameters.cs)。

对于#2,类似下面的内容应该可以解决问题:

private bool VisitExpression(Expression expression, Expression parentExpression, Statement parentStatement, CsElement parentElement, object context)
{
    if (expression.ExpressionType == ExpressionType.VariableDeclarator)
    {
        VariableDeclaratorExpression declaratorExpression = (VariableDeclaratorExpression)expression;
        if (declaratorExpression.Initializer == null)
        {
            this.AddViolation(parentElement, expression.LineNumber, "YourRule", declaratorExpression.Identifier.Text);
        }
    }

    return true;
}

现有的 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:

private bool VisitExpression(Expression expression, Expression parentExpression, Statement parentStatement, CsElement parentElement, object context)
{
    if (expression.ExpressionType == ExpressionType.VariableDeclarator)
    {
        VariableDeclaratorExpression declaratorExpression = (VariableDeclaratorExpression)expression;
        if (declaratorExpression.Initializer == null)
        {
            this.AddViolation(parentElement, expression.LineNumber, "YourRule", declaratorExpression.Identifier.Text);
        }
    }

    return true;
}

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.

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