c# - 如何在CQLinq中进行分组?

标签 c# ndepend cqlinq

我有一个自动生成的接口(interface)和实现,它定义了 3000 多个方法,并为每个方法定义了各自的异步签名。总共有 6,000 多种方法。

我想弄清楚哪些没有被使用。这是我迄今为止拥有的 CQLinq:

// Unused methods in the interface
let notUsed1 = new HashSet<IMethod>( 
from t in JustMyCode.Types 
where t.Name == "IDataProcessor"
from m in t.Methods 
where !m.HasAttribute("xyz.CoreService.CoreServiceOperationAttribute") &&  !m.MethodsCallingMe.Any()
select m)
// Unused methods in the concrete implementation of the interface
let notUsed2 =
from t in JustMyCode.Types 
where t.Name == "DataProcessor"
from m in t.Methods 
where m.HasAttribute("System.CodeDom.Compiler.GeneratedCodeAttribute") && !m.MethodsCallingMe.Any()
// Obtain the methods in the intersection
select m
from m in notUsed2 where notUsed1.Contains(m.OverriddensBase.Single())
let baseName = m.SimpleName.Replace("Async", "")
group m by baseName into g
select new { g } 

唉,这行不通。错误信息是:

Ln 18  Col 8  Type {IGrouping`2<String,IMethod>} not accepted to type first result argument.
Only IMethod, IField, IType, INamespace, IAssembly, IMember, ICodeElement, ICodeElementParent, ICodeContainer, IIssue and IRule are accepted to type first result argument.

看起来group是不可能的。我的想法是按基本名称(对于同步和异步签名相同)对方法进行分组,并选择具有 2 个项目的条目。但看来我的计划行不通,因为不支持分组。

你会怎么做?

最佳答案

我设法通过将分组键更改为 IMethod 对象来解决这个问题:

// Collect the sync methods
let syncMethods = (
from t in JustMyCode.Types 
where t.Name == "IDataProcessor"
from m in t.Methods 
where !m.HasAttribute("xyz.CoreService.CoreServiceOperationAttribute") && !m.IsAsync
select m).ToDictionary(m => m.SimpleName)
// Unused methods in the interface
let notUsed1 = new HashSet<IMethod>( 
from t in JustMyCode.Types 
where t.Name == "IDataProcessor"
from m in t.Methods 
where !m.HasAttribute("xyz.CoreService.CoreServiceOperationAttribute") && !m.MethodsCallingMe.Any()
select m)
// Unused methods in the concrete implementation of the interface
let notUsed2 =
from t in JustMyCode.Types 
where t.Name == "DataProcessor"
from m in t.Methods 
where m.HasAttribute("System.CodeDom.Compiler.GeneratedCodeAttribute") && !m.MethodsCallingMe.Any()
// Select methods in the intersection
select m
from m in notUsed2 where notUsed1.Contains(m.OverriddensBase.Single())
let syncMethod = syncMethods[m.SimpleName.Replace("Async", "")]
group m by syncMethod into g     // group by the respective sync method
where g.Count() == 2     // return if both sync and async signatures are not used
select new { g.Key }

我想知道如何简化它。

关于c# - 如何在CQLinq中进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65863089/

相关文章:

c# - 如何限制类型属性的 NDepend 方法查询

ndepend - ndepend 和 cppdepend 中的 CQL 可查看各个修订版本中指标的变化

c# - 如何以编程方式创建 NDepend 项目文件?

c# - 使用 NDepend 查找方法的所有使用(包括通过接口(interface))

ndepend - 为什么 NDepend 将静态字段计为方法的 LOC

c# - 什么是顶级类型和嵌套级别类型?

c# - WPF 图像滑动以像在 iOS 中一样更改图像

c# - .Net Core Async Await - 未按预期运行

c# - 如何像在 excel 列中一样创建字母表