如何编写代码分析 (FxCop) 规则以防止方法调用
如何为 vs2008 编写代码分析工具以防止特定框架调用,例如 GC.WaitForFullGCComplete() 或 Application.DoEvents()
我尝试覆盖自定义规则中的 VisitMethodCall,但我无法弄清楚 Microsoft.FxCop 的内容是什么.Sdk.MethodCall参数确实有在里面。 在网上找不到示例。
有人能指出我正确的方向吗?
How do I write a code analysis tool for vs2008 to prevent a specific framework call, such as GC.WaitForFullGCComplete() or Application.DoEvents()
I tried overriding the VisitMethodCall in my custom rule, but I cannot figure out what the Microsoft.FxCop.Sdk.MethodCall parameter really has in it. Could not find example on the web.
Can someone point me in the correct direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 System.Web.HttpUtility.HtmlEncode(System.String) 替换为您尝试查找和阻止的方法签名。
Replace
System.Web.HttpUtility.HtmlEncode(System.String)
with the method signature that you are trying to find and prevent.我记得一本调试书我认为其中有一章是关于 FXCop 规则的,并讨论了如何编写规则。 我一生都找不到我的副本。 我怀疑你需要反光镜
I remember a Debugging book that I think that had a chapter on FXCop rules and discussed how to write one. For the life of me I cannot find my copy. And I suspect you'll need reflector
解决编写 FxCop 规则的混乱问题的另一种方法是使用工具 NDepend。 这个工具可以让我们编写基于 C# LINQ 查询的代码规则 我们所说的 CQLinq< /a>. 免责声明:我是该工具的开发人员之一
超过200个代码默认情况下建议规则。 借助众所周知的 C# LINQ 语法,自定义现有规则或创建自己的规则非常简单。
对于这种特殊需求,简单的以下 CQLinq 规则就足够了(注意 AllowNoMatch() 使此规则适用于任何情况):
CQLinq 查询可以在 VisualStudio 中实时编辑,并提供即时结果,并具有结果浏览功能:
让我们准确地说,规则可以在 Visual Studio 中实时和构建过程中进行验证一次,在生成的 HTML+javascript 报告中。
An alternative to the mess of writing FxCop rules would be to use the tool NDepend. This tool lets write Code Rules over C# LINQ Queries what we call CQLinq. Disclaimer: I am one of the developers of the tool
More than 200 code rules are proposed by default. Customizing existing rules or creating your own rules is straightforward thanks to the well-known C# LINQ syntax.
For this particular need, the simple following CQLinq rule is enough (notice the usage of AllowNoMatch() to make this rule work in any situation):
CQLinq queries can be edited live in VisualStudio, and offer instant result, with result browsing facilities:
Let's precise that rules can be verified live in Visual Studio and at Build Process time, in a generated HTML+javascript report.
虽然它无法与 NDepend 甚至 FxCop 本身相匹敌,但我刚刚在这里发布了一种非常轻量级的方法,该方法对于防止调用特定方法等简单的事情非常有效:http://activesharp.codeplex.com/wikipage?title=使用%20ActiveSharp%20as%20a%20Unit%20Test %20工具
While it's no match for NDepend, or even FxCop itself, I've just posted a very lightweight approach here, that works well for simple things like preventing calls to particular methods: http://activesharp.codeplex.com/wikipage?title=Using%20ActiveSharp%20as%20a%20Unit%20Test%20Tool