将 C# 公共字段重构为属性的工具

发布于 2024-07-24 12:04:27 字数 111 浏览 5 评论 0原文

我有很多使用公共字段的 C# 代码,我想将它们转换为属性。

我有 Resharper,它会一一完成它们,但这将需要很长时间。

有谁知道可以帮助解决此问题的自动重构工具?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪化雨蝶 2024-07-31 12:04:28

Resharper 使用 Alt+PageDown / ALt+Enter(使用默认键绑定)非常快地完成此操作。 如果您位于第一个字段,Alt+PageDown 将跳转到下一个字段(因为它将包括包装公共字段作为建议的重构),并且 Alt+Enter 将提示您将其包装在属性中。

由于您很可能希望避免对所有属性进行全面包装,因此这可能是最快的方法。 对一个类执行此操作非常快,因为它准确地跳转到您需要去的地方......

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...

有木有妳兜一样 2024-07-31 12:04:28

将字段重构为属性(无需扩展):

将 C# 字段重构为属性

第 1 步:重构所有字段以由属性封装

第 2 步:将所有属性重构为自动实现的属性

Refactor fields to properties (no extensions needed):

Refactor C# fields to properties

Step 1: Refactor all fields to be encapsulated by properties

Step 2: Refactor all properties into auto-implemented properties

放肆 2024-07-31 12:04:28

如果您在 VS.NET 中重命名字段,VS 会提示您更改所有出现的重命名字段。

因此,将您的公共变量更改为属性名称,告诉 VS 更改该变量的所有实例,然后创建一个私有变量来存储该值和属性名称的公共属性。 删除公共变量,你就可以开始了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文