开发者

C# - How to convert old properties to shorthand properties in VS.net

开发者 https://www.devze.com 2023-02-19 14:05 出处:网络
I have legacy code with properties defined as : private string name; public st开发者_运维技巧ring Name

I have legacy code with properties defined as :

private string name;
public st开发者_运维技巧ring Name
{
   Get { return name;}
   Set { name = value;}
}

....

Is there a way I can select a complete class and convert it to the new shorthand version?

public string Name { get; set;}


Personally, I use the ReSharper VS plug-in; it can detect a basic property with backing field, like this, and suggest it be made an auto-property.


Buy or Try ReSharper. This can refactor this both ways one at a time.


If you are feeling extremely courageous you could use Ctrl+H (find and replace) and use a regular expression to find the old style properties.


Well... there's no easy way. And infact, if you use PropertyChange or DataErrorsChanged, you're going to wish that they were the old way.


If you use ReSharper it will highlight your Name property and give you the option to "Convert to Auto Property". Select the option and it converts your code automatically.

There is also an option to perform Full Code Cleanup, automatically implementing all ReSharper suggestions on your class by pressing Ctrl+E+C. Awesome!

If you aren't familiar with ReSharper, check it out here:

http://www.jetbrains.com/resharper/

It is the first thing I install after setting up Visual Studio 2008 or 2010!


CodeRush can do this to all properties in a class in one step.

I'm not sure whether the free version can do it, but it's worth a shot. The paid version can definitely do it.


Here's the regexp I typically use in Find and Replace:

{public [_:a]+ [_:a]+}\n\t+\{\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+
\1 { get; set; }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号