Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Adhoc Refactoring with Visual Studio's Replace in Files

2.25/5 (3 votes)
13 Apr 2009CPOL2 min read 13.8K  
One of my favorite refactoring tools is Visual Studio's Replace in Files dialog

Introduction

One of my favorite refactoring tools is Visual Studio's "Replace in Files" dialog. It pops up with the touch of the Ctrl+H key combo, sports all kinds of options, and operates against just the current file or set of files.

The most powerful option is the 'Use Regular Expressions' option. There are a few examples in MSDN to help get you started using regular expressions in Visual Studio. From there, you can quickly build up your own regex refactoring expressions library.

Using the Code

Example: Convert a set of automatic properties to the older style that will work in Visual Studio 2005.

This can be handy if you have code in a VS 2008 project that you would like to reuse in VS 2005. If you only have one or two automatic variables, let the compiler catch them and quickly make the changes by hand. But if they are scattered throughout many files, a few pre-canned regex expressions can decimate the amount of manual work required.

Steps

  1. Open an empty code file and paste in the sample code below:
    C#
    class Claim
    {
        // Works on multi line properties
        string PayerName 
        { 
            get;
            set;
        }
        // Or single line properties
        string PayerId { get;set;}
        string PayerIdQual { get;set;}
    }
  2. Hit Ctrl+H to bring up the Find in Files dialog. Make sure that Quick Replace is selected.
  3. Paste the regular expressions below into the Find What and Replace With text boxes:
    Find What: ^{:Wh*}{:i}:Wh*{:i}:Wh*\{:Wh*get:Wh*;:Wh*set:Wh*;:Wh*\}
    Replace With: \1private \2 m_\3;\n\1\public \2 \3 
    		{ get { return m_\3; } set { m_\3 = value; }}
  4. Click "Replace All".

Result

C#
class Claim
{
    private string m_PayerName;
    public string PayerName { get { return m_PayerName; } set { m_PayerName = value; }}
    private string m_PayerId;
    public string PayerId { get { return m_PayerId; } set { m_PayerId = value; }}
    private string m_PayerIdQual;
    public string PayerIdQual { get { return m_PayerIdQual; } 
				set { m_PayerIdQual = value; }}
} 

Points of Interest

Generic Properties

You may have asked yourself, "What happens if I have an automatic property with a private or protected setter or the property is a generic collection?" As the scenarios become more complex, decisions and tradeoffs begin to enter the picture.

The regex can be coded to handle multiple cases, but it can quickly become hard to understand and maintain. However going with 'lots of little' regexes can lead to different maintenance issues and it is a hassle to cut and paste all these regexes into the Find and Replace text boxes (however it can still be a huge productivity increase when you have to refactor hundreds of test cases in a hurry).

The next article will look at the strategies and alternative designs available with Visual Studio.

In either case, it is not too difficult to modify regexes once you understand how they work. You will find that with a little practice you can quickly create very powerful expressions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)