如何使用C#6.0为.NET2.0编译?

如何使用C#6.0为.NET2.0编译?,c#,.net,compilation,visual-studio-2015,c#-6.0,C#,.net,Compilation,Visual Studio 2015,C# 6.0,Visual Studio 2015使用新的c#编译器为旧的CLR进行编译没有问题。它似乎在幕后使用了VBCSCompiler.exe,但我找不到任何关于VBCSCompiler.exe命令行选项的文档 另一方面,csc.exe似乎没有选择目标CLR的选项。您可以使用最新的csc.exe为CLR 4编译,也可以使用较旧的csc.exe为CLR 2编译,但它不会是C#6 那么如何为CLR2和c#6.0编译呢?我必须要有visual studio才能完成此任务吗?还有其他选项吗?您可以使用/r指定旧

Visual Studio 2015使用新的c#编译器为旧的CLR进行编译没有问题。它似乎在幕后使用了VBCSCompiler.exe,但我找不到任何关于VBCSCompiler.exe命令行选项的文档

另一方面,csc.exe似乎没有选择目标CLR的选项。您可以使用最新的csc.exe为CLR 4编译,也可以使用较旧的csc.exe为CLR 2编译,但它不会是C#6


那么如何为CLR2和c#6.0编译呢?我必须要有visual studio才能完成此任务吗?还有其他选项吗?

您可以使用
/r
指定旧的.NET程序集:

 /reference:<alias>=<file>     Reference metadata from the specified assembly
                               file using the given alias (Short form: /r)
 /reference:<file list>        Reference metadata from the specified assembly
                               files (Short form: /r)
总之,这使得您可以使用C#6编译器构建.NET2.0应用程序

csc.exe /r:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll" /nostdlib Program.cs
你甚至可以在应用程序中使用C#6功能!(只要它们是不涉及.NET运行时的编译器专用功能)


我只想提出一个强制性的警告,即2.0不再受支持。
csc.exe /r:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll" /nostdlib Program.cs
public static string MyProp { get; } = "Hello!";
static void Main(string[] args)
{
    Console.WriteLine(MyProp);
    // prints "Hello!"

    var assembly = Assembly.GetAssembly(typeof(Program));
    Console.WriteLine(assembly.ImageRuntimeVersion);
    // prints "v2.0.50727"
}