Daniel Cazzulino's Blog : How to transform old properties to automatic properties with a simple search and replace

Subscriptions

News

Source code published in this blog is public domain unless otherwise specified.

 

kzu in LinkedIn

  Microsoft MVP Profile

 Contact

Post Categories

How to transform old properties to automatic properties with a simple search and replace

Say you have a (typically autogenerated) class with properties like:

public partial class Project : IExtensibleDataObject
{
    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
    {
        get
        {
            return this.extensionDataField;
        }
        set
        {
            this.extensionDataField = value;
        }
    }

Now you can fire up the Find & Replace dialog in VS and enter the following "simple" expression in Find What:

\n:b*\{[:b\n]*get[:b\n]*\{[.:b\n]*.*[.:b\n]*\}[.:b\n]*set[:b\n]*\{[.:b\n]*.*[.:b\n]*\}[.:b\n]*\}

And use the following expression for the replace:

 { get; set; } 

image

Don't forget to set the Use: Regular expressions option.

Running it will get you automatic properties for the entire file (or files):

public partial class Project : IExtensibleDataObject
{
    public System.Runtime.Serialization.ExtensionDataObject ExtensionData { get; set; }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public int AffiliateId { get; set; }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public Galleries.Domain.Model.Category[] Categories { get; set; }

 

Yeah, I know, that expression looks like crap ;)

posted on Tuesday, January 12, 2010 12:50 PM by kzu

# How to transform old properties to automatic properties with a ‘simple’ search and replace @ Tuesday, January 12, 2010 12:56 PM

Say you have a (typically autogenerated) class with properties like: public partial class Project : IExtensibleDataObject

Anonymous

# re: How to transform old properties to automatic properties with a simple search and replace @ Wednesday, January 13, 2010 3:57 AM

You need to point out that u need to search for the backing field and delete it by hand to check that all the code in the class uses the property :)

If u forget a backing field and the code use it u will break the code :P

Cheers

Marcos

# re: How to transform old properties to automatic properties with a simple search and replace @ Wednesday, January 13, 2010 10:40 AM

I just use ReSharper. :)

Bill Sorensen

# re: How to transform old properties to automatic properties with a simple search and replace @ Friday, January 15, 2010 4:47 AM

Warning: Such a replacement will actually destroy properties that do something in their getter/setter, so don't hit *Replace All*.

anon