如何限制 NDepend 方法查询类型属性

发布于 2024-08-23 09:04:57 字数 716 浏览 1 评论 0原文

我试图让 NDepend 使用标准“方法太大”查询的修改版本来识别长方法。

我不想报告开发人员几乎无法控制的长方法,因此我使用 DebuggerNonUserCode 属性和 InitializeComponent() 过滤掉生成的代码。

不幸的是,我仍然收到一些误报,因为生成类型中的方法也被报告。问题在于,虽然类型本身具有 DebuggerNonUserCode 属性,但方法却没有,因此尽管它们是生成的,但它们仍包含在输出中。

我正在寻找类型和方法之间的联接之类的东西:给我所有没有 DebuggerNonUserCode 属性的类型并对它们运行查询,但我不知道如何在其中表达这一点CQL。

对于某些程序集,我可以简单地过滤全名,但不幸的是,我们的一些程序集混合了开发人员制作和生成的类型。不幸的是,在这种情况下也不能使用 IsGenerateByCompiler

我的查询

WARN IF Count > 0 IN SELECT METHODS WHERE 
   NbLinesOfCode > 30 AND
   !HasAttribute "System.Diagnostics.DebuggerNonUserCodeAttribute" AND
   !NameIs "InitializeComponent()"
   ORDER BY NbLinesOfCode DESC

I'm trying to get NDepend to identify long methods using a modified version of the standard "Methods too big" query.

I don't want to report long methods that the developers have little control over, so I filter out generated code using the DebuggerNonUserCode attribute and InitializeComponent().

Unfortunately, I still get a few false positives as methods in generated types are reported as well. The problem is that while the type itself has the DebuggerNonUserCode attribute, the methods do not, so they are included in the output despite the fact that they are generated.

I am looking for something like a join between types and methods: Give me all types that do not have the DebuggerNonUserCode attribute and run the query on those, but I can't figure out how to express this in CQL.

For some of the assemblies, I can simply filter on the full name, but unfortunately some of our assemblies mix developer made and generated types. Unfortunately the IsGeneratedByCompiler can't be used either in this case.

My query

WARN IF Count > 0 IN SELECT METHODS WHERE 
   NbLinesOfCode > 30 AND
   !HasAttribute "System.Diagnostics.DebuggerNonUserCodeAttribute" AND
   !NameIs "InitializeComponent()"
   ORDER BY NbLinesOfCode DESC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难以启齿的温柔 2024-08-30 09:04:57

Brian,感谢 Code Rule over LINQ Query (CQLinq) 代码的源代码您要求的规则是:

warnif count > 0
from m in Application.Methods where
  m.NbLinesOfCode > 30 &&
 !m.ParentType.HasAttribute( "System.Diagnostics.DebuggerNonUserCodeAttribute") &&
  m.Name != "InitializeComponent()"
orderby m.NbLinesOfCode descending
select new { m, m.NbLinesOfCode }

CQLinq 语法还提供了一种定义“Just-My-Code”的方法。 此处描述了此功能。基本上,您需要通过前缀为 notmycode 的查询来定义集合 JustMyCode。然后,您要求的规则可以轻松重写:

warnif count > 0
from m in JustMyCode.Methods where
   m.NbLinesOfCode > 30
orderby m.NbLinesOfCode descending    
select new { m, m.NbLinesOfCode }

一次性定义的集合 JustMyCode 可以在任何代码规则上重用。另外,您可以查看 notmycode 默认查询 丢弃生成的和来自 JustMyCode 的设计器方法

Brian, thanks to Code Rule over LINQ Query (CQLinq) the source code of the code rule you are asking for is:

warnif count > 0
from m in Application.Methods where
  m.NbLinesOfCode > 30 &&
 !m.ParentType.HasAttribute( "System.Diagnostics.DebuggerNonUserCodeAttribute") &&
  m.Name != "InitializeComponent()"
orderby m.NbLinesOfCode descending
select new { m, m.NbLinesOfCode }

The CQLinq syntax also offer a way to define what is Just-My-Code. This feature is describe here. Basically you need to define the set JustMyCode through queries prefixed with notmycode. Then the rule you are asking for can be rewritten easily:

warnif count > 0
from m in JustMyCode.Methods where
   m.NbLinesOfCode > 30
orderby m.NbLinesOfCode descending    
select new { m, m.NbLinesOfCode }

The set JustMyCode defined once for all can be reused over any code rule. Additionaly you can look at the notmycode default query Discard generated and designer Methods from JustMyCode

缪败 2024-08-30 09:04:57

我非常喜欢 NDepend,但命名空间/类型/方法信息无法连接到单个查询中仍然是最大的缺点。该功能将使 CQL 变得非常强大。

除此之外,检查“IsGenerateByCompiler”和“IsInFrameworkAssembly”可能会有所帮助。
您还可以从查询中删除某些命名空间(OUT OF NAMESPACES "..."

I quite like NDepend, but it's still the biggest single shortcoming that namespace/type/method info cannot be joined into a single query. That feature would make CQL really powerful stuff.

Apart from that the checks 'IsGeneratedByCompiler' and 'IsInFrameworkAssembly' may be helpful.
You can also remover certain namespaces from the query (OUT OF NAMESPACES "...")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文