簡體   English   中英

StyleCop 自定義規則:方法參數和變量

[英]StyleCop Custom Rules: Method Parameters and Variables

我是 StyleCop 的新手,我需要為我工作的地方實施自己的編碼標准。 我正在使用 VS2005 並且無法調試它。 現在升級到 VS2008/2010 不是我們的選擇。

我想知道很多事情:

1)如何識別方法參數? 我嘗試了以下但不知道 go 的位置,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 個 class 以及如何獲取它們的標識符(名稱)?

真的:

 namespace Hello 
  {
     class P{

     }
  } 

- 錯誤的:

namespace Hi {
  class C {

  } 
  class E {

  }
}  
namespace Ho {
  class D {

  }
}

4) 我怎樣才能找到 function 電話並找出去哪里? (即阻止對特定函數的調用)

對於 #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 規則,因為它與源代碼樣式無關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM