我有一个针对旧版.NET Framework(.NET 4.5.2)的项目.我安装了Visual Studio 2015(因此我的机器上安装了.NET 4.6).我注意到如果我使用.NET 4.6/C#6中发布的C#语言功能,它仍会编译.如果我的项目的目标框架是<.NET 4.6,那么这不应该编译:
public string MyExpressionBodyProperty => "1";
//auto properties are new in C# 6
public string MyAutoProperty { get; } = "1";
private static void MethodThatUsesNameOf(string filename)
{
if (filename == null)
{
//nameof was released in C# 6
throw new ArgumentException("The file does not exist.", nameof(filename));
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能确保我只使用与我所针对的框架版本一起使用的.NET语言功能?