ndepend - NDepend 如何计算委托(delegate)的参数数量?

标签 ndepend

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

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

定义具有 5 个参数的委托(delegate)时,例如:

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

然后,质量约束会破坏源代码中不存在的函数(但可能在编译代码中),并且有 2 个附加参数:

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

如何解决这个障碍?

最佳答案

CQL 无法轻易解决这个问题,但是Code Rule over LINQ (CQLinq)自 NDepend v4 开始发布。

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

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

// <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 }

关于ndepend - NDepend 如何计算委托(delegate)的参数数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10914491/

相关文章:

c# - 为什么方法缺乏凝聚力(LCOM)包括getters和setters

ndepend - 将旧分析添加到 NDepend 并指定日期

build - 如何让 NDepend 作为构建过程的一部分运行?

design-patterns - 在NDepend中构建CQL以验证MVVM模式

.net - 退出痛苦地带 - NDepend

c# - 仅保留使用过的类型并删除未使用的类型

.net - 解决 MSB3247 - 发现同一依赖程序集的不同版本之间存在冲突

visual-studio-2010 - Visual Studio 中分离/附加的 NDepend 窗口不记得它们应该在哪里

asp.net-mvc - ASP.NET MVC 应用程序有哪些好的静态代码分析规则? (N依赖)

.net - 确定未使用的方法和属性