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
How to Autogenerate multiple getters/setters or accessors in Visual Studio
提问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
回答by Mitchel Sellers
回答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,它很棒但很商业化)能够快速做到这一点。


