Search Amazon:

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

 

Custom FxCop Rule - Do not use VB6 Conversion Functions

This custom FxCop rule looks to see that you have not used the older VB6 conversion functions like CStr, CDate, CInt etc. Instead, you should be using Convert.ToString(), Convert.ToDateTime(), Convert.ToInt32(), ...

If you create a VB.NET sample program with the above conversion calls, when you build the code and examine the MSIL, you will see calls to:

      [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions

This rule looks for calls to the CompilerServices.Conversions namespace.

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 DoNotUseVB6Conversions : BaseMigrationControlFlowRule
{
    public DoNotUseVB6Conversions() : base("DoNotUseVB6Conversions")
    {
    }

    // Check for 'CompilerServices.Conversions' in the generated Intermediate Language.
    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() ==
             "MICROSOFT.VISUALBASIC.COMPILERSERVICES.CONVERSIONS")
        {
            base.Problems.Add(new Problem(GetResolution(), programContext));
            //
            // Can output IL statement w/ this parameter. 
            // Must add {0} placeholder to Rules.xml <Resolution> value.
            //
            //base.Problems.Add(new Problem(GetResolution(calleeName), programContext));
        }
        base.VisitCall(destination, receiver, callee, arguments, 
            isVirtualCall, programContext, stateBeforeInstruction, 
            stateAfterInstruction);
    }
}

Rule definition in the XML rules file:

<Rule TypeName="DoNotUseVB6Conversions" 
          Category="VBMigration" CheckId="AA1001">
    <Name>
        Do not use the VB6 conversion functions
    </Name>
    <Description>
         Do not use the VB6 data conversion functions  
         such as CStr, CInt, CDate, etc.
    </Description>
    <Url>
        http://www.thescarms.com/</Url>
    <Resolution>
        Do not use the VB6 conversion functions. Use the 'Convert.To...' 
        methods from the 'System.Convert' namespace instead.
    </Resolution>
    <MessageLevel Certainty="99">
        Warning</MessageLevel>
    <FixCategories>
        NonBreaking
    </FixCategories>
    <Owner />
<Rule>

 

Page 4 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:39:54 PM By:  PSMACCHIA
With the CQL language provided with NDepend, coding this rule is as simple as:

WARN IF Count > 0 IN SELECT METHODS WHERE IsDirectlyUsing "Microsoft.VisualBasic.CompilerServices.Conv ersions"

http://www.NDepend.com
 
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