繁体   English   中英

Resharper 将自动属性转换为完整属性

[英]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));
}

所需的步骤是:

  • 在您的视图模型所在的项目中安装 Nuget package “JetBrains.Annotations”
  • 在引发PropertyChanged事件的方法上使用[NotifyPropertyChangedInvocator]属性
  • 之后,“带有更改通知的属性”选项可用于您的视图模型的属性

在此处输入图像描述


另请参阅: Jetbrains INotifyPropertyChanged 支持

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM