visual-studio 使用 Visual Studio 2008/2010 或 Resharper 5 创建自动属性的快捷方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2974920/
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
Shortcut to create automatic properties using Visual Studio 2008/2010 or Resharper 5
提问by Piers Myers
I have a class that contains a load of properties that contain results of some calculations e.g:
我有一个包含大量属性的类,其中包含一些计算的结果,例如:
public class Results
{
public double Result1 { get; set; }
public double Result2 { get; set; }
}
In a different class I am doing calculations to populate the above properties, e.g:
在另一个类中,我正在计算以填充上述属性,例如:
public class Calc
{
private Results Calc()
{
Results res = new Results();
res.Result1 = ... some calculation
res.Result2 = ... some other calculation
res.Result3 = ... // not yet defined in 'Results' class
return res;
}
}
When I am writing the Calcclass, Result3will be highlighted in red as it is not yet defined in the Resultsclass.
当我编写Calc类时,Result3将以红色突出显示,因为它尚未在Results类中定义。
Currently I am using the Resharper ALT+ Entershortcut, selecting "Create Property 'Result3'" which will create the following code int the Resultsclass:
目前我正在使用 Resharper ALT+Enter快捷方式,选择“创建属性 'Result3'”,这将在Results类中创建以下代码:
public double Result3
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
Which I need to manually change to:
我需要手动更改为:
public double Result3 { get; set; }
Then I use the CTRL+ Shift+ Backspaceshortcut to take me back to the Calcclass.
然后,我用的是CTRL+ Shift+Backspace快捷键带我回Calc班。
How can I easily create automatic properties in the Resultsclass if they are not yet defined directly from the Calcclass?
Results如果尚未直接从Calc类中定义,如何在类中轻松创建自动属性?
采纳答案by Brett Veenstra
Sounds like you'd like to stay in the Calcclass and create your properties in Resultsin one go.
听起来您想留在Calc课堂上并Results一次性创建您的属性。
Here's what you do.
这就是你要做的。
Turn on Solution-wide error checking (Resharper 5 is considerably better/faster)
Edit
Calcreferring to allResultsproperties, leaving the errors reported.Hit Alt+ Shift+ PageDown. That will cycle you through all your errors.
Press Alt+ Enterand choose Create Property and use the Auto-Property choice in
Results, then hit Alt+ Shift+ PageDownto get back toCalc
打开解决方案范围的错误检查(Resharper 5 更好/更快)
编辑
Calc引用所有Results属性,留下报告的错误。点击Alt+ Shift+ PageDown。这将使您遍历所有错误。
按Alt+Enter并选择 Create Property 并使用 中的 Auto-Property 选项
Results,然后按Alt+ Shift+PageDown返回Calc
回答by Neil Barnwell
For Resharper 4/5, type propthen tab, then fill in the required parameters.
对于 Resharper 4/5,键入propthen tab,然后填写所需的参数。
回答by David M
As soon as you've pressed Alt+ Enterto create the property, and then Enterto accept the data type, you actually get a drop-down list of ways of implementing it if you're on a recent version of ReSharper. One of them is auto property.
一旦您按下Alt+Enter来创建属性,然后Enter接受数据类型,如果您使用的是最新版本的 ReSharper,您实际上会得到一个实现方式的下拉列表。其中之一是汽车财产。

