【问题标题】:How does NDepend count the parameters number for delegates?NDepend 如何计算代表的参数数量?
【发布时间】:2012-06-06 12:45:28
【问题描述】:

我们使用 NDEPEND CQL 请求对代码设置了一些质量约束:

WARN IF Count > 0 IN SELECT TOP 10 METHODS WHERE NbParameters > 6

当定义一个带 5 个参数的委托时,例如:

delegate void MyDelegate(IArg arg1, IArg arg2, IArg arg3, IArg arg4, IArg arg5);

然后质量约束在源代码中不存在(但可能在编译代码中)并具有 2 个附加参数的函数上中断:

BeginInvoke(IArg, IArg, IArg, IArg, IArg, AsyncCallback,Object)

如何解决这个障碍?

【问题讨论】:

    标签: ndepend


    【解决方案1】:

    CQL 不能轻易解决这个问题,但自 NDepend v4 发布的Code Rule over LINQ (CQLinq) 可以。

    CQLinq 带有定义什么是 JustMyCode 的工具,因此消除了生成的方法,例如 BeginInvoke(IArg, IArg, IArg, IArg, IArg, AsyncCallback,Object)。对此进行了解释:Defining the code base view JustMyCode with notmycode prefix

    基本上默认和可自定义的代码规则Discard generated Types from JustMyCode 丢弃委托类型及其方法,因为它们总是被生成的。

    // <Name>Discard generated Types from JustMyCode</Name>
    // --- Make sure to make this query richer to discard generated types from NDepend rules results ---
    notmycode
    from t in Application.Types where
    
      // Resources, Settings, or typed DataSet generated types for example, are tagged with this attribute
      t.HasAttribute ("System.CodeDom.Compiler.GeneratedCodeAttribute".AllowNoMatch()) ||
    
      // Delegate types are always generated
      t.IsDelegate ||
    
      // Discard ASP.NET page types generated by aspnet_compiler.exe
      // See: http://www.ndepend.com/FAQ.aspx#ASPNET
      t.ParentNamespace.Name.EqualsAny("ASP", "__ASP") ||
    
      // Discard generated type ContractException
      t.Name == "__ContractsRuntime+ContractException" ||
      t.FullName == "System.Diagnostics.Contracts.RuntimeContractsAttribute" ||
      t.FullName == "System.Diagnostics.Contracts.__ContractsRuntime" ||
    
      // Discard all types declared in a folder path containing the word "generated"
      (t.SourceFileDeclAvailable &&
       t.SourceDecls.All(s => s.SourceFile.FilePath.ParentDirectoryPath.ToString().ToLower().Contains("generated")))
    
    select new { t, t.NbILInstructions }
    

    【讨论】:

      猜你喜欢
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2020-03-19
      • 2019-06-20
      • 2011-05-24
      • 1970-01-01
      • 2010-09-11
      相关资源
      最近更新 更多