Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Code Analysis (FxCop) rule to prevent a method call

Tags:

.net

fxcop

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?

like image 599
Bill Avatar asked Mar 16 '09 01:03

Bill


1 Answers

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;
    }
like image 118
slolife Avatar answered Nov 13 '22 20:11

slolife