2D Head with a clock as an eyeball.
 Tuesday, June 26, 2007

I recently posted on the evils of Microsoft.VisualBasic.MsgBox and have been looking at ways of preventing future problems to do with it. I decided on using static analysis to warn developers that they are using MsgBox rather than MessageBox.Show, so I thought I would share how I went about it.

Writing custom FxCop rules is reasonably well documented, and there are some excellent resources available for the inexperienced and experienced FxCop developer alike:

Of course the rules available in Team System and later versions of FxCop are written in .NET and therefore are very instructive to reflect using something like Reflector. However the process for writing rules can be summed up as such.

   1.   Decide if you are targeting FxCop or VS Team System Code analysis. Because they run different versions, you will have to compile your rules against the appropriate dll's. The good news is that development is largely the same from here on in.

   2.   Create a new class library project in Visual Studio and reference the FxCopSdk.dll and the Microsoft.Cci.dll. You will find these in your FxCop installation directory or in the case of VSTS you will find them in [VSInstallDir]\Team Tools\Static Analysis Tools\FxCop\.

   3.   Write your rule such that it inherits from Microsoft.FxCop.Sdk.Introspection.BaseIntrospectionRule. You may find it useful to create your own base rule class as I did:

      using System;
      using Microsoft.FxCop.Sdk.Introspection;

      namespace Nervoustych.CodeAnalysis
      {
         [CLSCompliant(false)]
         public abstract class BaseStaticAnalysisRule : BaseIntrospectionRule
         {
            protected BaseStaticAnalysisRule(string name) : 
               base(name, "Nervoustych.CodeAnalysis.Rules.Rules"
               typeof(BaseStaticAnalysisRule).Assembly)
            {

            }

            //other base class methods here
         }
      }

    Note the string literal parameter in the constructor; it refers to an embedded XML File resource that describes the rule to the FxCop engine. David Kean has a top example of a base rule class on his site.

   4.   Create the aforementioned XML file and embed it as a resource in your project. The rules document uses a schema called FxCopReport.xsd which is in the Xml directory under the FxCop 1.35 installdir. Here is an example of a Rules.xml file containing one rule.

   <?xml version="1.0" encoding="utf-8" ?> 
   <Rules FriendlyName="Nervoustych Rules"> 
      <Rule TypeName="DoNotUseMsgBox" Category="Nervoustych.Design" CheckId="NT0001"> 
         <Name>Full name of rule</Name
         <Description>Rule description</Description
         <Resolution Name="DoNotUseMsgBox">Resolution Description</Resolution
         <FixCategories>NonBreaking</FixCategories>
         <MessageLevel Certainty="99">Warning</MessageLevel
         <Email></Email
         <Url></Url
         <Owner></Owner
      
</Rule
   </Rules>

   5.   Write the rule. In my rule I wanted to warn developers of uses of MsgBox, so I needed to check any method calls for MsgBox:

   using System;
   using Microsoft.Cci; 
   using Microsoft.FxCop.Sdk; 
   using Microsoft.FxCop.Sdk.Introspection; 

   namespace Nervoustych.CodeAnalysis.Rules 
   { 
      class DoNotUseMsgBox : BaseStaticAnalysisRule 
      { 
         public DoNotUseMsgBox() : base("DoNotUseMsgBox") { } 
         public override ProblemCollection Check(Member member) 
         { 
            Method lMethod = member as Method; 
            if (lMethod == null) return base.Check(member); 
            foreach (Instruction i in lMethod.Instructions) 
            { 
               if (IsMethodCall(i) 
                  && ((Method)i.Value).FullName.Contains("Microsoft.VisualBasic.MsgBox")) 
               { 
                  Problems.Add(new Problem(GetNamedResolution("DoNotUseMsgBox"),
                     i.SourceContext)); 
               } 
            } 
            return Problems; 
         } 

         private static bool IsMethodCall(Instruction instruction) 
         { 
            if (instruction == null
               throw new ArgumentNullException("instruction"); 
            switch (instruction.OpCode) 
            { 
               case OpCode.Callvirt: 
               case OpCode.Call: 
               case OpCode.Calli: 
                  return true
               default
                  return false
            } 
         } 
      } 
   }

As you can see the override to the Check method cycles through each instruction in a method and checks its operation code, to confirm if it's a method call to Microsoft.VisualBasic. MsgBox or not. If it is, then a problem is added to the collection using BaseIntrospectionRule.GetNamedResolution. If anyone could let me know a better way to do this so that I don't have to use a string literal, I would be forever grateful.

   6.   Build your rules project and copy the resultant assembly to either [VSInstallDir]\Team Tools\Static Analysis Tools\FxCop\Rules or [FxCopInstallDir]\Rules depending on your target platform. FxCop will pick up rule assemblies from this directory. At this point you are ready to start using your custom rule against target assembliles using either the FxCop command line, GUI or VSTS code analysis.

   7.   Didn't quite work? If you need to debug your rule, then you will probably want to read this.

    All in all, it's not really that hard, the worst part is learning your way around the undocumented introspection engine. However reflecting the existing rules really helps, and there is a thriving community of mentors to reach out to if the going gets tough. Happy rule making.


    Filed under:  |  | 
    | Comments [3] | Trackback
    Tracked by:
    "FxCop This" (DotNetKicks.com) [Trackback]
    http://morningside.edu/mics/_notes/pages/rainbow-brite/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/prilosec/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/effexor/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/synthroid/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/prozac/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/coumadin/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/lexapro/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/prilosec/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/celebrex/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/hoodia/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/viagra/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/accutane/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/claritin/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/lipitor/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/wellbutrin/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/cialis/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/celebrex/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/viagra/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/lexapro/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/prozac/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/paxil/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/ultram/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/tramadol/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/celexa/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/wellbutrin/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/tramadol/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/hoodia/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/lipitor/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/effexor/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/nexium/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/claritin/index.html [Pingback]
    http://morningside.edu/mics/_notes/pages/clomid/index.html [Pingback]
    http://blastpr.com/wiki/js/pages/nexium/index.html [Pingback]
    http://hrvatska.biz/wp-includes/js/docs/80692203/index.html [Pingback]
    http://ncdtnanotechportal.info/generator/docs/87198700/index.html [Pingback]
    http://coolioness.com/attachments/docs/75395149/index.html [Pingback]
    http://pddownloads.com/docs/66275653/index.html [Pingback]
    http://thebix.com/includes/compat/docs/10152421/index.html [Pingback]
    http://swellhead.netswellhead.net/docs/84545083/index.html [Pingback]
    http://seo4u.at/images/docs/76783685/index.html [Pingback]
    http://jivest2006.com/docs/76826750/index.html [Pingback]
    http://vladan.strigo.net/wp-includes/js/docs/04726190/index.html [Pingback]
    http://martinrozon.com/images/photos/docs/75270452/index.html [Pingback]
    http://promocija.com.hr/promocija.com.hr/includes/js/docs/52060005/index.html [Pingback]
    http://martinrozon.com/images/photos/docs/61904307/index.html [Pingback]
    http://vladan.strigo.net/wp-includes/js/docs/09763218/index.html [Pingback]
    http://witze-humor.de/templates/images/docs/69259068/index.html [Pingback]
    http://pspdesktops.com/fileupload/store/docs/18769945/index.html [Pingback]
    http://slaterjohn.com/downloads/2col/51579700/index.html [Pingback]
    http://blog.netmedia.hr/wp-includes/js/docs/84238305/index.html [Pingback]
    http://promocija.com.hr/promocija.com.hr/includes/js/docs/63224938/index.html [Pingback]
    http://promocija.com.hr/promocija.com.hr/includes/js/docs/70471394/index.html [Pingback]
    http://pddownloads.com/docs/94929363/index.html [Pingback]
    http://thejohnslater.com/pix/img/docs/73486930/index.html [Pingback]
    http://allfreefilms.com/wp-includes/js/25891222/index.html [Pingback]
    http://discussgod.com/cpstyles/docs/43932298/index.html [Pingback]
    http://split-dalmatia.com/split-dalmatia.com/images/docs/84431573/index.html [Pingback]
    http://thebix.com/includes/compat/docs/15132509/index.html [Pingback]
    http://islands-croatia.comislands-croatia.com/includes/js/docs/54089144/index.ht... [Pingback]
    http://islands-croatia.comislands-croatia.com/includes/js/docs/82710340/index.ht... [Pingback]
    http://witze-humor.de/templates/images/docs/83157240/index.html [Pingback]
    http://swellhead.netswellhead.net/docs/92808772/index.html [Pingback]
    http://temerav.com/images/menu/91084644/index.html [Pingback]
    http://jivest2006.com/docs/40579018/index.html [Pingback]
    http://add2rss.com/img/design/docs/73396176/index.html [Pingback]
    http://thejohnslater.com/pix/img/docs/86193101/index.html [Pingback]
    http://add2rss.com/img/design/docs/90861918/index.html [Pingback]
    http://entartistes.ca/images/images/docs/28212733/index.html [Pingback]
    http://realestate.hr/templates/css/docs/28593877/index.html [Pingback]
    http://plantmol.com/docs/60217277/index.html [Pingback]
    http://promocija.com.hr/promocija.com.hr/includes/js/docs/48335156/index.html [Pingback]
    http://ipsilon.hr/ipsilon.hr/cms/4/lib/docs/55227677/index.html [Pingback]
    http://ipsilon.hr/ipsilon.hr/cms/4/lib/docs/24066563/index.html [Pingback]
    http://coolioness.com/attachments/docs/60340594/index.html [Pingback]
    http://allfreefilms.com/wp-includes/js/27702077/index.html [Pingback]
    http://slaterjohn.com/downloads/2col/66689432/index.html [Pingback]
    http://pddownloads.com/docs/21991908/index.html [Pingback]
    http://add2rss.com/img/design/docs/45658867/index.html [Pingback]
    http://discussgod.com/cpstyles/docs/90092602/index.html [Pingback]
    http://pspdesktops.com/fileupload/store/docs/33460308/index.html [Pingback]
    http://legambitdufou.org/Library/docs/04618667/index.html [Pingback]
    http://easytravelcanada.info/js/pages/12/wellbutrin/ [Pingback]
    http://sevainc.com/bad_denise/img/12/zoloft/ [Pingback]
    http://easytravelcanada.info/js/pages/2/celexa/ [Pingback]
    http://easytravelcanada.info/js/pages/11/ultram/ [Pingback]
    http://easytravelcanada.info/js/pages/4/cymbalta/ [Pingback]
    http://birds.sk/img/cialis/ [Pingback]
    http://easytravelcanada.info/js/pages/12/zoloft/ [Pingback]
    http://sevainc.com/bad_denise/img/6/lexapro/ [Pingback]
    http://simpletravelcanada.info/js/pages/27277365/ [Pingback]
    http://sevainc.com/bad_denise/img/10/soma/ [Pingback]
    http://abaffy.net/i/img/viagra/ [Pingback]
    http://sevainc.com/bad_denise/img/5/effexor/ [Pingback]
    http://easytravelcanada.info/js/pages/12/viagra/ [Pingback]
    http://easytravelcanada.info/js/pages/1/celebrex/ [Pingback]
    http://easytravelcanada.info/js/pages/9/prozac/ [Pingback]
    http://ina-tv.sk/img/viagra/ [Pingback]
    http://adventure-traveling.com/images/img/viagra/ [Pingback]
    http://easytravelcanada.info/js/pages/11/tramadol/ [Pingback]
    http://easycanada.info/js/pages/viagra/ [Pingback]
    http://sevainc.com/bad_denise/img/8/prilosec/ [Pingback]
    http://sevainc.com/bad_denise/img/12/wellbutrin/ [Pingback]
    http://easytravelcanada.info/js/pages/9/rainbow-brite/ [Pingback]
    http://sevainc.com/bad_denise/img/7/melatonin/ [Pingback]
    http://easytravelcanada.info/js/pages/10/soma/ [Pingback]
    http://easytravelcanada.info/js/pages/5/hoodia/ [Pingback]
    http://sevainc.com/bad_denise/img/11/ultram/ [Pingback]
    http://inatelevizia.sk/ad/img/cialis/ [Pingback]
    http://easymexico.info/images/img/cialis/ [Pingback]
    http://sevainc.com/bad_denise/img/9/prozac/ [Pingback]
    http://easytravelcanada.info/js/pages/6/lipitor/ [Pingback]
    http://sevainc.com/bad_denise/img/1/accutane/ [Pingback]
    abaffy.org/la/img/viagra/ [Pingback]
    http://easytravelcanada.info/js/pages/8/paxil/ [Pingback]
    abaffy.org/la/img/cialis/ [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/35807953/taylor-hayes-free-pics.htm... [Pingback]
    http://cidesi.com/images/metro/metro2/pages/99493954/index.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/99493954/porn-postcards-free.html [Pingback]
    http://odin.net/images/pages/52807681/free-online-porn-samples.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/53348735/jacqueline-teen-model-is-n... [Pingback]
    http://odin.net/images/pages/35694472/free-adult-sex-classifieds-china.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/53348735/sexy-makeup-pics.html [Pingback]
    http://odin.net/images/pages/35694472/small-tit-teens-tgp.html [Pingback]
    http://odin.net/images/pages/35694472/jenny-maccarthy-nude.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/35807953/mature-fucking-movies.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/35807953/milking-tits-escorts.html [Pingback]
    http://odin.net/images/pages/35694472/index.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/99493954/sex-gadis-melayu.html [Pingback]
    http://odin.net/images/pages/52807681/diaper-scat.html [Pingback]
    http://odin.net/images/pages/52807681/drug-test-shop-penis.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/35807953/teenage-girl-nude.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/53348735/ametuer-zoo-girls.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/99493954/cheerleader-erotic-stories.... [Pingback]
    http://cidesi.com/images/metro/metro2/pages/32162341/girl-teen-underwear.html [Pingback]
    http://odin.net/images/pages/52807681/are-baby-walkers-bad.html [Pingback]
    http://odin.net/images/pages/52807681/index.html [Pingback]
    http://odin.net/images/pages/52807681/bollywood-actress-in-bikini-bra.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/53348735/aunt-judy-porn-site.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/32162341/nude-fake-celebs-pics.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/53348735/what-is-the-mature-ripened... [Pingback]
    http://cidesi.com/images/metro/metro2/pages/99493954/mmf-free-sex-sites.html [Pingback]
    http://odin.net/images/pages/52807681/hot-sexy-horny-slut-fucking.html [Pingback]
    http://odin.net/images/pages/35694472/time-square-webcam.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/99493954/statistics-on-teens-allowan... [Pingback]
    http://odin.net/images/pages/35694472/cartoon-penis.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/99493954/adult-porn-comic.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/32162341/xpress-train-hentai-movie.h... [Pingback]
    http://odin.net/images/pages/35694472/hottest-movie-sex-scenes.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/35807953/busty-ebony-retro-sylvia-m... [Pingback]
    http://odin.net/images/pages/52807681/all-fours-thumbnail-naked-girl.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/53348735/adult-swim-crest.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/32162341/a1-thumbnails-posts.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/32162341/vip-adult-clubs.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/99493954/oops-babes.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/35807953/xxx-asian-anal-milf-free.h... [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/53348735/celebrities-sexy-pictures.... [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/53348735/free-sex-positions-clips.h... [Pingback]
    http://odin.net/images/pages/52807681/marathon-florida-webcam.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/99493954/erotic-comic-archives.html [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/53348735/bikini-dare-pics.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/99493954/kid-sex.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/32162341/penis-too-small.html [Pingback]
    http://odin.net/images/pages/35694472/gay-justin-berfield.html [Pingback]
    http://odin.net/images/pages/35694472/kate-winslet-nude-scenes-in-jude.html [Pingback]
    http://odin.net/images/pages/35694472/bikini-calenders.html [Pingback]
    http://cidesi.com/images/metro/metro2/pages/32162341/i-need-free-party-ideas-for... [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/35807953/pictures-of-black-girls.ht... [Pingback]
    http://gatewayplayhouse.com/photos/cai/pages/53348735/granny-movie-thumbs.html [Pingback]
    © Copyright 2008 Jim Burger