Welcome to AspAdvice Sign in | Join | Help

Refactor Request

Not sure if it's already there, but the folks at DevExpress or JetBrains (or Microsoft, but I don't want to wait for another VS) should have a refactoring for CodeRush/Refactor! or Resharper that will convert verbose properties into C# 3.5 short properties, like so:

Make this:

protected bool isSponsored = false;
public bool IsSponsored
{
    get { return isSponsored; }
    set { isSponsored = value; }
}

Into this:

public bool IsSponsored { get; set; }

 

Ideally, it should work in two forms:

1) Right click on the property (field name let's say) and offer it as an option in that context.

2) Apply to all properties in a class.

And it should only do it if it is safe, of course.  If I had set isSponsored to true in the original code, the refactoring wouldn't have been possible, since it would have changed the behavior.

Of course, if some enterprising developer has already done this, please comment with a link.  Maybe Keyvan's done it - he just published a book on VS extensibility...

Published Wednesday, April 02, 2008 1:12 PM by ssmith
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Refactor Request

Wednesday, April 02, 2008 2:02 PM by Dustin Campbell

# re: Refactor Request

Even though Dustin said it's a part of Refactor but it would be a good idea for blogging.

Just added it to my TO BLOG list ;-)

Wednesday, April 02, 2008 2:54 PM by Keyvan Nayyeri

# re: Refactor Request

I submitted a feature request to DevExpress for you - http://www.devexpress.com/Support/Center/p/S91058.aspx

Wednesday, April 02, 2008 4:27 PM by Zack Jones

# re: Refactor Request

Thursday, April 03, 2008 1:54 AM by Lothan

# re: Refactor Request

At the risk of splitting hairs, your example won't quite work, as your backing field is protected, whereas the auto-implemented fields will be private; but the refactoring is still a valid one.

Thursday, April 03, 2008 2:50 AM by David Keaveny

# re: Refactor Request

@Dustin/@Zack - Great, so it's just a user training issue (I need to find the feature).

@David - Good catch.  It's possible the change from protected to private would have ill effects.  It wouldn't, in the case I ran into that prompted this post, but that could make the refactoring unsafe.  Hopefully the IDE tools have considered this.

Thursday, April 03, 2008 9:17 AM by ssmith

# re: Refactor Request

@David: It's definitely refactorable. Just produce this:

public bool IsSponsored { get; protected set; }

Thursday, April 03, 2008 9:25 AM by Dustin Campbell

# re: Refactor Request

Hey Steve, with regard to discovering this refactoring, just place the caret at the start of the property you want to convert and you'll see the smart tag, which means you can hover over it, or you can press the refactor key to see the refactoring menu with the "Convert to Auto-implemented Property" refactoring (among others) available.

Thursday, April 03, 2008 5:20 PM by Mark Miller

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below