Search Amazon:

Page 2 of 10               << Previous  1  2  3  4  5  6  7  8  9  10  Next >>

 

Custom FxCop Rule - Do not call .NET Garbage Collector

Here is the CSharp source code for a custom FxCop rule to enforce not calling the .NET Garbage Collector directly from code. This rule, named DoNotCallGarbageCollectorDirectly inherits from the BaseMigrationControlFlowRule class and calls the VisitCall method.

If you create a small VB.NET sample program to call the .NET Framework's Garbage Collector, build the code, and examine the MSIL, you will see calls to:

[mscorlib]System.GC::GetTotalMemory
[mscorlib]System.GC::CollectionCount
[mscorlib]System.GC::SuppressFinalize

So, we can create an FxCop rule that looks for calls to System.GC which is the class. We do not need to look for the particular methods.

Sample VB.NET code to call the garbage collector:

          Dim totMem As Long = GC.GetTotalMemory(True)
          Dim colCount As Integer = GC.CollectionCount(0)
          GC.SuppressFinalize(Me)

C# code for custom FxCop rule:

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

public class DoNotCallGarbageCollectorDirectly : BaseMigrationControlFlowRule
{
    public DoNotCallGarbageCollectorDirectly() : base("DoNotCallGarbageCollectorDirectly")
    {
    }

    public override void VisitCall(Variable destination, 
               Variable receiver, Method callee, ExpressionList arguments, 
               bool isVirtualCall, IProgramContext programContext, 
               IExecutionState stateBeforeInstruction, 
               IExecutionState stateAfterInstruction)
    {
        if (callee.DeclaringType.FullName.Trim().ToUpper().Equals("SYSTEM.GC"))
        {
            base.Problems.Add(new Problem(GetResolution(), programContext));
        }
        base.VisitCall(destination, receiver, callee, arguments, 
                isVirtualCall, programContext, stateBeforeInstruction, 
                stateAfterInstruction);
   }
}

Rule definition in the XML rules file:

<Rule TypeName="DoNotCallGarbageCollectorDirectly" 
          Category="VBMigration" CheckId="AA1001">
          
    <Name>
        Do not call Garbage Collector directly
    </Name>
    <Description>
        Calls to the Garbage Collector directly are not advised.
    </Description>
    <Url>
        http://www.thescarms.com/
    </Url>    
    <Resolution>
        Do not call Garbage Collector directly. To ensure objects are 
        disposed of properly, use the Using keyword.
    </Resolution>
    <MessageLevel Certainty="99">
        Warning
    </MessageLevel>
    <FixCategories>
        NonBreaking
    </FixCategories>
    <Owner />
<Rule>

The XML rules file is mostly self explanatory. You have the rule name which must match the class name, category the rule falls in so FxCop can group the results of its analysis, a certainty level, which in this case is set high since we can positively identify GC calls, and a recommended resolution. The CheckId is a unique id for the rule. Each rule must have a different CheckId.

 

Page 2 of 10               << Previous  1  2  3  4  5  6  7  8  9  10  Next >>

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Comments: 1 Items to Show:     

     
Id:  1 Posted:  5/6/2007 4:32:25 PM By:  PSMACCHIA
Folks interested in FxCop should have a glance at the tool NDepend:
http://www.NDepend.com

NDepend analyses source code and .NET assemblies. It allows controlling the complexity, the internal dependencies and the quality of .NET code.


NDepend provides a language (CQL Code Query Language) dedicated to query and constraint a codebase.

For example, to code this rule it would be as simmple as:
WARN IF Count>0 IN SELECT METHODS WHERE IsDirectlyUsing 'System.GC'
 
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms