[英]Resharper convert auto-property to full property
我在這里發現了相反的情況,但我需要經常從自動屬性更改為完整屬性 - 是否可以自動執行(也許使用快捷方式):
public string FirstName { get; set; }
private string _firstName;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
顯然,我會進一步改變整個財產......
將光標放在屬性名稱上,然后等待一兩秒鍾。 按Resharper熱鍵序列(Alt-Enter),第二個選項應為“To property with backing field”,這是您想要的。
或者,您可以單擊左邊距中的“錘子”圖標以獲取選項。
要使其工作(ALT-Enter),您必須配置resharper鍵盤架構。 VS - >工具 - >選項 - > ReSharper - >常規 - >選項 - >鍵盤和菜單 - > Resharper鍵盤架構 - > Visual Studio
我想添加一個擴展解決方案,它還允許以這種樣式創建一個屬性,它支持視圖模型的INotifyPropertyChanged
:
public string Name
{
get => _name;
set
{
if (value == _name) return;
_name = value;
RaisePropertyChanged();
}
}
/// <summary>
/// Raises the <see cref="PropertyChanged"/> event for the given <paramref name="propertyName"/>.
///
/// The <see cref="NotifyPropertyChangedInvocatorAttribute"/> attribute is used
/// because of Resharper support for "to property with change notification":
/// https://www.jetbrains.com/help/resharper/Coding_Assistance__INotifyPropertyChanged_Support.html
/// </summary>
/// <param name="propertyName"></param>
[NotifyPropertyChangedInvocator]
public virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
所需的步驟是:
PropertyChanged
事件的方法上使用[NotifyPropertyChangedInvocator]
屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.