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 >>




About TheScarms
About TheScarms


Sample code
version info

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

Email this page


© Copyright 2024 TheScarms
Goto top of page