Tools for refactoring C# public fields into properties

天大地大妈咪最大 提交于 2019-12-06 02:25:16

问题


I have a lot of C# code that uses public fields, and I would like to convert them to properties.

I have Resharper, and it will do them one by one, but this will take forever.

Does anyone know of an automated refactoring tool that can help with this?


回答1:


Resharper does it very quickly, using Alt+PageDown / ALt+Enter (with the default key bindings). If you are at the first field, Alt+PageDown will jump to the next one (since it'll include wrapping public fields as a suggested refactoring), and Alt+Enter will prompt you to wrap it in a property.

Since you most likely want to avoid a full blanket wrapping of all properties, this is probably the quickest approach. It's quite fast to do this to a class, since it jumps exactly where you need to go...




回答2:


If you're in VS.NET when you rename a field, VS prompts you to change all occurrences of the renamed field.

So change your public Variable to the property name, tell VS to change all the instances of this variable, then create a private variable to store the value and a public property of the Proper name. Delete the public Variable and you should be good to go.



来源:https://stackoverflow.com/questions/1028679/tools-for-refactoring-c-sharp-public-fields-into-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!