NDepend Query显示const,enum,struct依赖项

时间:2015-06-03 08:41:09

标签: ndepend

我编写了以下查询以确定我的解决方案与其他程序集之间的依赖关系。我们有一个内部Nuget共享库的大型库,它被广泛使用,我想确保包含这些库 - 因此我使用了' t'下面是为了消除某些第三方程序集,但包括我们的内部库。

这个查询效果很好,但我已经意识到它只显示了依赖项是方法调用的依赖项。它不包括常量,枚举和结构。

如何增强下面的查询以向我们展示这些以及任何其他依赖项的详细信息?

let t = Assemblies.WithNameWildcardMatchIn("xunit","RestSharp","NSubstitute","O2S*","EntityFramework","AxInterop*","AutoMapper","Autofac*","ADODB","mscorlib","System*", "Microsoft*","Infra*","Interop*").ToDictionary<IAssembly,String>(c=>c.Name)
from a in Application.Assemblies
from m in a.ChildMethods
from b in m.MethodsCalled
let isThirdParty = t.ContainsKey(b.ParentAssembly.Name)

select new { a,isThirdParty,m.ParentNamespace, m.ParentType,m.Name,DependsOnAssembly=b.ParentAssembly.Name, DependsOnNamespace=b.ParentNamespace,DependsOnParentType=b.ParentType,DependsOnMethod=b.Name}

1 个答案:

答案 0 :(得分:0)

您的查询的重构版本如何:

from a in Application.Assemblies
from m in a.ChildMethods
from b in m.MethodsCalled.Cast<IMember>().Union(m.FieldsUsed.Cast<IMember>())
let isThirdParty = b.IsThirdParty

select new { 
   a,
   isThirdParty,
   m.ParentNamespace, 
   m.ParentType,
   m.Name,

   DependsOnAssembly=b.ParentAssembly.Name, 
   DependsOnNamespace=b.ParentNamespace,
   DependsOnParentType=b.ParentType,
   DependsOnMember=b.Name
}

首先,我们使用b.IsThirdParty:)

大大简化了它

其次,我们在Union<IMember>()MethodsCalled之间进行FieldsUsed。因此,除了被称为方法之外,您还可以读取和/或分配所有字段。

关于结构用法,只要你使用结构的成员(构造函数,属性,字段......),就会列出依赖关系。

关于枚举,如果方法使用枚举,您将看到对实例字段EnumName.value__的依赖关系。

但是你不会看到常量和枚举值的使用。原因是这些信息在NDepend分析的IL代码中丢失了。常量(和枚举值也是常量)将替换为IL代码中的值。

希望这有帮助!

作为旁注,如果您改为编写,则查询结果在NDepend UI中的可读性更高:

from m in Application.Methods
select new { 
   m,
   thirdPartyMethodsCalled = m.MethodsCalled.Where(m1 => m1.IsThirdParty),
   applicationMethodsCalled = m.MethodsCalled.Where(m1 => !m1.IsThirdParty),
   thirdPartyFieldsUsed = m.FieldsUsed.Where(m1 => m1.IsThirdParty),
   applicationFieldsUsed = m.FieldsUsed.Where(m1 => m1.IsThirdParty)
}

NDepend dependencies code query method field usage