visual-studio 如何在 Visual Studio 中自动生成多个 getter/setter 或访问器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/85928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:31:59  来源:igfitidea点击:

How to Autogenerate multiple getters/setters or accessors in Visual Studio

visual-studiovisual-studio-2008idecode-generation

提问by Mario

Before I start, I know there is this post and it doesn't answer my question: How to generate getters and setters in Visual Studio?

在我开始之前,我知道有这篇文章,但它没有回答我的问题:如何在 Visual Studio 中生成 getter 和 setter?

In Visual Studio 2008 there is the ability to auto generate getters and setters (accessors) by right clicking on a private variable -> Refactor -> Encapsulate Field...

在 Visual Studio 2008 中,可以通过右键单击私有变量 -> 重构 -> 封装字段来自动生成 getter 和 setter(访问器)。

This is great for a class that has 2 or 3 methods, but come on MS! When have you ever worked with a class that has a few accessors?

这对于有 2 或 3 个方法的类来说非常棒,但是来吧 MS!你什么时候用过一个有几个访问器的类?

I am looking for a way to generate ALL with a few clicks (Eclipse folks out there will know what I am talking about - you can right click a class and select 'generate accessors'. DONE.). I really don't like spending 20 minutes a class clicking through wizards. I used to have some .NET 1.0 code that would generate classes, but it is long gone and this feature should really be standard for the IDE.

我正在寻找一种只需单击几下即可生成 ALL 的方法(Eclipse 人员会知道我在说什么 - 您可以右键单击一个类并选择“生成访问器”。完成。)。我真的不喜欢在一节课上花 20 分钟点击向导。我曾经有一些会生成类的 .NET 1.0 代码,但它早已不复存在,这个功能应该真正成为 IDE 的标准。

UPDATE: I might mention that I have found Linq to Entities and SQLMetal to be really cool ideas, and way beyond my simple request in the paragraph above.

更新:我可能会提到我发现 Linq to Entities 和 SQLMetal 是非常酷的想法,远远超出了我在上面段落中的简单要求。

回答by Rasmus Faber

Sorry, you really need to install Resharperto get approximately the same amount of refactoring support as you are used to in Eclipse.

抱歉,您确实需要安装Resharper才能获得与您在 Eclipse 中使用的大致相同数量的重构支持。

However, Resharper gives you a dialog very similar to the one you are used to in Eclipse: Screenshot

但是,Resharper 为您提供了一个与您在 Eclipse 中习惯使用的对话框非常相似的对话框: 截屏

回答by Mitchel Sellers

I have an "info class generator" application that you can use an excel sheet and it will generate the private members and the public get/set methods.

我有一个“信息类生成器”应用程序,您可以使用 Excel 表,它将生成私有成员和公共 get/set 方法。

You can download it for free from my website.

你可以从我的网站上免费下载。

回答by Eddie Deyo

In 2008 I don't bother with Encapsulate Field. I use the new syntax for properties:

在 2008 年,我不打扰 Encapsulate Field。我对属性使用新语法:

public string SomeString { get; set; }

回答by petr k.

Possibly a macro. There are also addins (like ReSharper, which is great but commercial) capable of doing that quickly.

可能是一个宏。还有一些插件(如 ReSharper,它很棒但很商业化)能够快速做到这一点。