2

我们使用 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)

如何解决这个障碍?

4

1 回答 1

2

CQL 不能轻易解决这个问题,但是NDepend v4 之后发布的基于 LINQ 的代码规则 (CQLinq)可以。

CQLinq 带有定义什么是JustMyCode的工具,因此消除了像BeginInvoke(IArg, IArg, IArg, IArg, IArg, AsyncCallback,Object)这样的生成方法。这在:Defining the code base view JustMyCode with notmycode prefix

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

// <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 }
于 2012-06-06T13:24:21.700 回答