Click here to Skip to main content
15,891,473 members
Articles / Operating Systems / Windows

FXCop 1.32

Rate me:
Please Sign up or sign in to vote.
4.43/5 (12 votes)
17 Oct 20059 min read 106.8K   2.2K   49  
Bad Code in .NET? FxCop to the rescue.
using System;
using System.Diagnostics;
using System.Reflection;
using Microsoft.FxCop.Sdk;
using Microsoft.FxCop.Sdk.Introspection;
using Microsoft.Cci;
using System.Text.RegularExpressions;


public class stringwithStr:BaseIntrospectionRule
    {
    public stringwithStr():
    base(@"stringwithStr", "DummyProj.DLADesignRules",
    typeof(stringwithStr).Assembly)
        {
	        //
	        // TODO: Add constructor logic here
	        //
        }
    
    public override ProblemCollection Check(Member member)
        {
            // Member Variables
 
            // Look for str in local variable names
            Method method = member as Method;
            if (method == null)
            {
                return null;
            }
            LocalList list = null;
            if (method.Instructions.Length > 0)
            {
                list = method.Instructions[0].Value as LocalList;
            }
            if (list != null)
            {
                for (int i = 0; i < list.Length; i++)
                {
                    Local local = list[i];

                   if (local.Type == SystemTypes.String)
                    {
                        if (ContainsStr(local.Name.Name))
                        {
                            // Found a local containing a numeric in the name
                            base.Problems.Add(new Problem(base.GetResolution(local.Name.Name), local.Name.Name));

                        }
                    }
                }
            }
  
            return base.Problems;
        }

    
        private bool ContainsStr(string variableName)
        {
             
            //NOTE:
            //Use regexp instaed of substring, which will be more faster.
                if (!(variableName.Substring(0, 3).ToString().Trim() == "str"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            
        }


    }
 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions