【问题标题】:Refactoring "Implement Interface" using auto properties VS2015使用自动属性 ​​VS2015 重构“实现接口”
【发布时间】:2015-09-02 22:43:02
【问题描述】:

我正在尝试让 Visual Studio 2015 (14.0) 在使用 C# 重构实现接口时使用自动属性。

即我想要这个;

public object SomeProperty { get; set; }

与此相反;

public object SomeProperty
{
    get
    {
        throw new NotImplementedException();
    }
    set
    {
        throw new NotImplementedException();
    }
}

我在过去版本的 Visual Studio 中通过编辑代码 sn-p 文件(指令 here)完成了此操作,但使用 Visual Studio 2015 无法使其工作。

【问题讨论】:

  • 我也做不到。我已经更改了我的 propertyStub.sn-p 但它没有注册更改。你解决了吗?
  • 不,看起来还没有解决方案。如果我有时间,我可能会为自己做一个扩展。
  • 谢谢@labilbe。我想我很久以前就发现了这个。令人失望,但希望将来会有解决方法或实施。

标签: c# visual-studio ide refactoring visual-studio-2015


【解决方案1】:

好的,所以我在测试 VS2019 Preview (16.0) 时偶然发现了答案。

在主菜单栏中Tools --> Options --> Text Editor --> C# --> Advanced 中查找Implement Interface or Abstract Class 下的选项When generating properties 选择prefer auto properties

这导致与 sn-ps 用于处理 VS2015 之前的结果相同。

【讨论】:

    【解决方案2】:

    您可以通过编辑PropertyStub.snippet来解决

    只需转到C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Refactoring 打开PropertyStub.snippet 并编辑:

    $GetterAccessibility$ get 
    { 
        $end$throw new $Exception$(); 
    }
    $SetterAccessibility$ set 
    { 
        throw new $Exception$(); 
    }
    

    $GetterAccessibility$ get;
    $SetterAccessibility$ set;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多