簡體   English   中英

使用Visual Studio 2008/2010或Resharper 5創建自動屬性的快捷方式

[英]Shortcut to create automatic properties using Visual Studio 2008/2010 or Resharper 5

我有一個包含大量屬性的類,其中包含一些計算結果,例如:

public class Results
{
    public double Result1 { get; set; }
    public double Result2 { get; set; }
}

在另一個類中,我正在進行計算以填充上述屬性,例如:

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;
    }
}

當我編寫Calc類時, Result3將以紅色突出顯示,因為它尚未在Results類中定義。

目前我正在使用Resharper ALT + Enter快捷鍵,選擇“Create Property'Round3'”,它將在Results類中創建以下代碼:

public double Result3
{
    get { throw new NotImplementedException(); }
    set { throw new NotImplementedException(); }
}

我需要手動更改為:

public double Result3 { get; set; }

然后我使用CTRL + Shift + Backspace快捷方式將我帶回Calc類。

如果尚未直接從Calc類定義,如何在Results類中輕松創建自動屬性?

對於Resharper 4/5,鍵入prop then tab,然后填寫所需的參數。

只要您按Alt + Enter創建屬性,然后按Enter鍵接受數據類型,如果您使用的是最新版本的ReSharper,則實際上會獲得一個實現方法的下拉列表。 其中一個是汽車財產。

聽起來你想留在Calc類中並一次性在Results中創建屬性。

這是你做的。

  1. 打開解決方案范圍的錯誤檢查(Resharper 5更好/更快)

  2. 編輯Calc引用所有Results屬性,並報告錯誤。

  3. 按Alt + Shift + PageDown 這將使您完成所有錯誤。

  4. Alt + Enter並選擇Create Property並使用Results的Auto-Property選項,然后按Alt + Shift + PageDown返回Calc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM