使用自动属性重构 "Implement Interface" VS2015

Refactoring "Implement Interface" using auto properties VS2015

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

即我想要这个;

public object SomeProperty { get; set; }

与此相反;

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

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

您可以通过编辑 PropertyStub.snippet

来解决

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

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

$GetterAccessibility$ get;
$SetterAccessibility$ set;

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

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

这会产生与用于处理 VS2015 之前的代码片段相同的结果。