如何编写代码分析 (FxCop) 规则以防止方法调用

发布于 2024-07-14 21:54:37 字数 218 浏览 6 评论 0原文

如何为 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 技术交流群。

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

发布评论

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

评论(4

错爱 2024-07-21 21:54:37

将 System.Web.HttpUtility.HtmlEncode(System.String) 替换为您尝试查找和阻止的方法签名。

    public override ProblemCollection Check(Member member)
    {
        if (member is Method)
        {
            var callees = new List<Method>(Callees.CalleesFor((Method)member));
            foreach (var callee in callees)
            {
                if (callee.FullName == "System.Web.HttpUtility.HtmlEncode(System.String)")
                {
                    Problems.Add(new Problem(GetResolution()));
                }
            }
        }

        return Problems;
    }

Replace System.Web.HttpUtility.HtmlEncode(System.String) with the method signature that you are trying to find and prevent.

    public override ProblemCollection Check(Member member)
    {
        if (member is Method)
        {
            var callees = new List<Method>(Callees.CalleesFor((Method)member));
            foreach (var callee in callees)
            {
                if (callee.FullName == "System.Web.HttpUtility.HtmlEncode(System.String)")
                {
                    Problems.Add(new Problem(GetResolution()));
                }
            }
        }

        return Problems;
    }
清音悠歌 2024-07-21 21:54:37

我记得一本调试书我认为其中有一章是关于 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

梦幻的心爱 2024-07-21 21:54:37

解决编写 FxCop 规则的混乱问题的另一种方法是使用工具 NDepend。 这个工具可以让我们编写基于 C# LINQ 查询的代码规则 我们所说的 CQLinq< /a>. 免责声明:我是该工具的开发人员之一

超过200个代码默认情况下建议规则。 借助众所周知的 C# LINQ 语法,自定义现有规则或创建自己的规则非常简单。

如何为vs2008编写代码分析工具来阻止特定框架调用,例如GC.WaitForFullGCComplete()或Application.DoEvents()

对于这种特殊需求,简单的以下 CQLinq 规则就足够了(注意 AllowNoMatch() 使此规则适用于任何情况):

// <Name>Don't call these methods</Name>
warnif count > 0
from m in Methods 
where m.IsUsing ("System.GC.WaitForFullGCComplete()".AllowNoMatch()) ||
      m.IsUsing ("System.GC.WaitForFullGCComplete(Int32)".AllowNoMatch()) ||
      m.IsUsing ("System.Windows.Forms.Application.DoEvents()".AllowNoMatch())
select m

CQLinq 查询可以在 VisualStudio 中实时编辑,并提供即时结果,并具有结果浏览功能:

enter此处的图像描述

让我们准确地说,规则可以在 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.

How do I write a code analysis tool for vs2008 to prevent a specific framework call, such as GC.WaitForFullGCComplete() or Application.DoEvents()

For this particular need, the simple following CQLinq rule is enough (notice the usage of AllowNoMatch() to make this rule work in any situation):

// <Name>Don't call these methods</Name>
warnif count > 0
from m in Methods 
where m.IsUsing ("System.GC.WaitForFullGCComplete()".AllowNoMatch()) ||
      m.IsUsing ("System.GC.WaitForFullGCComplete(Int32)".AllowNoMatch()) ||
      m.IsUsing ("System.Windows.Forms.Application.DoEvents()".AllowNoMatch())
select m

CQLinq queries can be edited live in VisualStudio, and offer instant result, with result browsing facilities:

enter image description here

Let's precise that rules can be verified live in Visual Studio and at Build Process time, in a generated HTML+javascript report.

各自安好 2024-07-21 21:54:37

虽然它无法与 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

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