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