Visual Studio 2017 的 "Implement Interface" 命令可以生成 C# 5 代码吗?
Can Visual Studio 2017's "Implement Interface" command generate C# 5 code?
在 Visual Studio 2017 年,在“Quick Actions and Refactorings”菜单中,当我单击“Implement Interface”时,我得到了使用 C# 新功能的实现,如下所示:
public int ConnectionTimeout => throw new NotImplementedException();
问题是我的公司使用的是 C# 5,所以我们不能使用这种较新的语法。我知道 Visual Studio 的旧版本会生成以下代码:
public int ConnectionTimeout { get { throw new NotImplementedException(); } }
有没有办法让 Visual Studio 2017 做到这一点?我已将我的项目设置为使用 C# 5 进行编译。
编辑:我注意到代码生成器不一致。如果接口有一个只有 getter 的 bool 成员,它使用旧语法。否则它使用新语法。这让我觉得我运气不好。
private interface myint
{
bool bool1 { get; }
bool bool2 { get; set; }
bool bool3 { set; }
int int1 { get; }
int int2 { get; set; }
int int3 { set; }
string string1 { get; }
string string2 { get; set; }
string string3 { set; }
}
private class myclass : myint //I clicked "Implement Interface" on this
{
public bool bool1
{
get
{
throw new NotImplementedException();
}
}
public bool bool2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public bool bool3 { set => throw new NotImplementedException(); }
public int int1 => throw new NotImplementedException();
public int int2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public int int3 { set => throw new NotImplementedException(); }
public string string1 => throw new NotImplementedException();
public string string2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public string string3 { set => throw new NotImplementedException(); }
}
转到 工具 -> 选项 -> 文本编辑器 -> 代码样式 -> 常规,滚动到 代码块首选项 部分,然后更改 For properties、For indexers 和 For accessors 来自 "Prefer expression block"(默认)到 "Prefer block body".
在 Visual Studio 2017 年,在“Quick Actions and Refactorings”菜单中,当我单击“Implement Interface”时,我得到了使用 C# 新功能的实现,如下所示:
public int ConnectionTimeout => throw new NotImplementedException();
问题是我的公司使用的是 C# 5,所以我们不能使用这种较新的语法。我知道 Visual Studio 的旧版本会生成以下代码:
public int ConnectionTimeout { get { throw new NotImplementedException(); } }
有没有办法让 Visual Studio 2017 做到这一点?我已将我的项目设置为使用 C# 5 进行编译。
编辑:我注意到代码生成器不一致。如果接口有一个只有 getter 的 bool 成员,它使用旧语法。否则它使用新语法。这让我觉得我运气不好。
private interface myint
{
bool bool1 { get; }
bool bool2 { get; set; }
bool bool3 { set; }
int int1 { get; }
int int2 { get; set; }
int int3 { set; }
string string1 { get; }
string string2 { get; set; }
string string3 { set; }
}
private class myclass : myint //I clicked "Implement Interface" on this
{
public bool bool1
{
get
{
throw new NotImplementedException();
}
}
public bool bool2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public bool bool3 { set => throw new NotImplementedException(); }
public int int1 => throw new NotImplementedException();
public int int2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public int int3 { set => throw new NotImplementedException(); }
public string string1 => throw new NotImplementedException();
public string string2 { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public string string3 { set => throw new NotImplementedException(); }
}
转到 工具 -> 选项 -> 文本编辑器 -> 代码样式 -> 常规,滚动到 代码块首选项 部分,然后更改 For properties、For indexers 和 For accessors 来自 "Prefer expression block"(默认)到 "Prefer block body".