首页 > 解决方案 > NDepend 查询以确定正在调用其他 DLL 中的哪些方法

问题描述

我们有一个非常大的代码库,多个团队拥有不同的层。一个团队想知道调用了哪些方法和类型,以便他们可以集中它们。因此,对于我们放在 NDepend 项目中的一组 DLL 和可执行文件,什么查询会为我们提供所有方法,这些方法将被使用并包含在以名称“Company.ODS”开头的程序集中。

标签: ndepend

解决方案


有两种方法可以编写此查询。

在这两种情况下let assembliesUsed = Application.Assemblies.WithNameIn("Infrastructure", "ApplicationCore")都是适应您的代码的部分,例如let assembliesUsed = Assemblies.Where(a => a.Name.StartsWith("CompanyName.Feature"))


A) 使用使用的类型/方法/字段呈现结果。

let assembliesUsed = Application.Assemblies.WithNameIn("Infrastructure", "ApplicationCore")

let typesUsed = assembliesUsed.ChildTypes().ToHashSetEx()
let membersUsed = assembliesUsed.ChildMembers().ToHashSetEx()

let typesUser = Application.Types.UsingAny(typesUsed).Where(
  t => !assembliesUsed.Contains(t.ParentAssembly))

let methodsUser = Application.Methods.UsingAny(membersUsed).Where(
  m => !assembliesUsed.Contains(m.ParentAssembly))

from x in assembliesUsed.ChildTypesAndMembers()
let users = 
  x.IsMethod ? x.AsMethod.MethodsCallingMe.Intersect(methodsUser).Cast<IMember>() : 
  x.IsField ?  x.AsField.MethodsUsingMe.Intersect(methodsUser).Cast<IMember>() : 
               x.AsType.TypesUsingMe.Intersect(typesUser)
where users.Any()
select new { x, users }

DLL 程序集耦合由使用的代码元素索引


B) 使用类型/方法用户呈现结果。

let assembliesUsed = Application.Assemblies.WithNameIn("Infrastructure", "ApplicationCore")

let typesUsed = assembliesUsed.ChildTypes().ToHashSetEx()
let membersUsed = assembliesUsed.ChildMembers().ToHashSetEx()

let typesUser = Application.Types.UsingAny(typesUsed).Where(
  t => !assembliesUsed.Contains(t.ParentAssembly))

let methodsUser = Application.Methods.UsingAny(membersUsed).Where(
  m => !assembliesUsed.Contains(m.ParentAssembly))

from x in methodsUser.Concat<IMember>(typesUser)

let used = 
  x.IsMethod ? x.AsMethod.MembersUsed.Intersect(membersUsed) : 
               x.AsType.TypesUsed.Intersect(typesUsed) 
where typesUsed.Any()
select new { x, used }

由用户代码元素索引的 DLL 程序集耦合


推荐阅读