xvdiff
xvdiff

Reputation: 2229

Automagically convert properties to auto getter/setter

I've generated a code base for service communication from a WSDL file which resulted in around 100 classes containing the following:

public class SomeClass {

    private string _someVar;

    public string SomeVar {
       get { return _someVar; }
       set { _someVar = value; }
    }

}

Is it possible to automagically turn everyone of them into auto properties? Maybe using ReSharper or some regex magic?

Upvotes: 0

Views: 457

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1503290

If you just need to do this once, you can let R# do it for you with a Code Cleanup action.

Right-click on the project (or solution, or single source file), select "Cleanup code..." and then use profile which includes "Use auto-property, if possible." (If you don't have such a profile already, you can edit your profiles from that dialog.)

However, I would strongly advise you to separate generated code from hand-written code. Make all your generated code use partial types (and potentially partial methods) - that way you can create a hand-written partial type which merges with the auto-generated code, without being in the same file. You don't need to look at the generated code, and you can replace it with another version later without worrying about any custom changes.

Upvotes: 1

Les
Les

Reputation: 10605

The following Visual Studio regex will find the pattern you have above when plugged into the "Find & Replace" tool.

    private:Wh*:i:Wh*:i;:Wh*public:Wh*(:i):Wh*(:i):Wh*\{:Wh*get:Wh*\{[^\}]*\}:Wh*set:Wh*\{[^\}]*\}:Wh*\}

And this pattern will do the replace.

    public \1 \2 { get; set; }

Upvotes: 0

Related Questions