C# 按依赖顺序对类进行排序(NDepend)

C# 按依赖顺序对类进行排序(NDepend),c#,ndepend,C#,Ndepend,我是独立学院的新手,刚刚开始学习。我能够使用以下简单查询创建类依赖关系图: from t in Application.Types // remove compiler generated classes where !t.FullName.Contains(">") // sort by dependency count orderby t.TypesUsed.Count() select new { t, t.TypesUsed } 我试图构建一个有序的类列表,这样列表中的第一个项就

我是独立学院的新手,刚刚开始学习。我能够使用以下简单查询创建类依赖关系图:

from t in Application.Types
// remove compiler generated classes
where !t.FullName.Contains(">")
// sort by dependency count
orderby t.TypesUsed.Count()
select new { t, t.TypesUsed }

我试图构建一个有序的类列表,这样列表中的第一个项就不会有系统类型以外的依赖项。当我们浏览列表时,每种类型都应该只有在列表中出现在它前面的依赖项。我意识到,在周期性依赖的情况下,这是不可能的,但我想至少让它们按更正确的顺序排列,而不是简单地按依赖计数排序。

这是一个复杂但可行的方法。这个CQLinq代码查询使用了NDepend.API方法的魔力

//调用FillIterative()将用已处理的类型填充哈希集
让hashSet=newhashset()
让fillHashSetWithTypesFunc=newfunc(类型=>types.All(t=>hashSet.Add(t)))
设metric=ThirdParty.Types.fills(
类型=>
//使用三元运算符调用总是返回true的fillHashSetWithTypesFunc(),
用typesFunc(类型)填充HashSetWithTypesFunc?
//当哈希集包含所有t.TypesUsed时,选择t
其中(t=>t.TypesUsed.Intersect(hashSet.Count()==t.TypesUsed.Count()):
//由于fillHashSetWithTypesFunc()始终返回true,因此从未对新的IType[0]进行开票
新的IType[0])
从val到公制
其中val.Value>0//不显示第三方类型
orderby val.值升序
选择新的{val.codelement,
(val.CodeElement作为IType)。级别,
价值,
(val.codelement作为IType).TypesUsed}
循环中涉及的类型不匹配

结果为匹配的每种类型指定一个值:

  • (1) 类型是否仅使用第三方类型
  • (2) 类型是否使用第三方和(1)中的类型
  • (3)
有趣的是,这个指标正好提供了这个度量并回答了您的问题。但是用代码查询重新编写它更有趣。名称空间(也具有级别度量)、程序集和方法也可以这样做

// The call FillIterative() will fill hashSet with types already processed
let hashSet = new HashSet<IType>()

let fillHashSetWithTypesFunc = new Func<IEnumerable<IType>, bool>(types => types.All(t => hashSet.Add(t)))

let metric = ThirdParty.Types.FillIterative(
   types => 
      // Use a ternary operator to invoke fillHashSetWithTypesFunc() that always return true,
      fillHashSetWithTypesFunc(types) ?
         // Select t when hashSet contains all t.TypesUsed
         Application.Types.Where(t => t.TypesUsed.Intersect(hashSet).Count() == t.TypesUsed.Count()) :
         // new IType[0] is never invoqued coz fillHashSetWithTypesFunc() always returns true
         new IType[0])

from val in metric 
where val.Value > 0  // Don't show third-party types
orderby val.Value ascending
select new { val.CodeElement, 
   (val.CodeElement as IType).Level, 
   val.Value, 
   (val.CodeElement as IType).TypesUsed }