C# 列出并统计类属性的所有用法

问题描述

我的解决方案中有几个具有很多属性的类。 现在我想写一段代码,可以输出一个包含以下信息的列表

  • 班级
  • 类属性
  • 解决方案中类属性的使用

有没有聪明的方法来做到这一点? (我知道如何列出类属性,但如何获取使用的属性及其计数?)

解决方法

使用工具 NDepend,您可以write a code query 与此类似。

            // Adjust this to match exactly the time you want
            // like for example ... WithNameLike("regex")
let types = Application.Namespaces.WithName("Nop.Core").ChildTypes()

let properties = (from t1 in types
   from m in t1.Methods
   where !m.IsStatic && m.IsPropertyGetter || m.IsPropertySetter
   select m)

from prop in properties
select new { prop,prop.MethodsCallingMe }

然后您可以浏览查询结果,并通过 prop.MethodsCallingMe 获取每个属性的使用计数和列表:

ndepend code query result

然后,您可以使用各种工具将查询结果中的部分或全部元素导出到实时 dependency graph,以更好地可视化依赖关系:

ndepend code query result exported to a dependency graph

你可以download full featured trial here

免责声明:我在 NDepend 工作

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...