使用 FxCop Introspection 进行代码分析的无限循环
我正在尝试编写自定义 FxCop 代码分析规则 这将警告开发人员注意包含嵌套太深的代码块的方法, 并将敦促他们重新解决混乱的问题。
前任。我试图避免以下情况:
if(condition)
{
foreach(var item in items)
{
if(anotherCondition)
{
for(var product in item.Products)
{
// even more nested statement blocks...
}
}
}
}
当我重写 VisitBlock(Block block)
方法时出现堆栈溢出
计算块的深度,因为显然存在循环引用 从块的属性之一到 块本身。 即对于某些 i,以下情况成立: block.Statements[i] == block
为什么存在这样的循环引用?如何避免呢? 谢谢!
I'm trying to write a custom FxCop code analysis rule
that will warn developers from methods containing too deeply nested code blocks,
and will urge them to re-factor out the mess.
ex. I'm trying to avoid the following situation:
if(condition)
{
foreach(var item in items)
{
if(anotherCondition)
{
for(var product in item.Products)
{
// even more nested statement blocks...
}
}
}
}
I get a stackoverflow when I override the VisitBlock(Block block)
method
that counts the block's depth, because apparently, there is a cyclic reference
from one of the properties of the block to
the block itself.
i.e. the following is true for some i: block.Statements[i] == block
Why does such a cyclic reference exist? How to avoid it?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过更多研究,我发现我实际上有两个主要问题
但实际访问生成的 IL 中的节点。只需比较每个方法生成的 IL 指令
以及每个 method.Body 生成的语句。
我想知道如果 FxCop 我们能取得什么成果
能为我们提供真正的 AST 访客吗?
代码块中,我们应该自己扫描方法代码,我的意思是,取出
method.Body
的SourceContext
属性中的起始行和结束行并保留每一个的轨迹我们找到了“{”和“}”。 “{”的递增计数器和“}”的递减计数器。那应该有效,对吧?
after some more research, I've figured out I had actually TWO main problems
but actually visit nodes in the generated IL. Just compare the generated IL instructions per method
and the generated statements per method.Body.
I wonder what we could have achieved if FxCop
could provide us with a true AST visitor?
code blocks, we should just scan the method code by ourselves, I mean, take out the start line and the end line inside the
SourceContext
property of themethod.Body
and keep track of every'{' and '}' we find. Increment counter for '{' and decrement counter for '}'. That should work, right?