【问题标题】:How do I compile for .NET 2.0 with C# 6.0?如何使用 C# 6.0 为 .NET 2.0 编译?
【发布时间】:2016-04-06 21:54:36
【问题描述】:
Visual Studio 2015 使用新的 c# 编译器为旧的 CLR 编译没有问题。似乎它在后台使用了 VBCSCompiler.exe,但我找不到任何关于 VBCSCompiler.exe 命令行选项的文档。
另一方面,csc.exe 似乎没有选择目标 CLR 的选项。您可以使用最新的 csc.exe,它将为 CLR 4 编译,或者您可以使用较旧的 csc.exe 为 CLR 2 编译,但它不会是 C# 6。
那么如何为 CLR 2 和 c# 6.0 编译?我必须为此拥有视觉工作室吗?还有其他选择吗?
【问题讨论】:
标签:
c#
.net
compilation
visual-studio-2015
c#-6.0
【解决方案1】:
您可以使用 /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)
您还需要使用 /nostdlib 禁止自动包含现代 mscorlib:
/nostdlib[+|-] Do not reference standard library (mscorlib.dll)
这些共同作用使您可以使用 C# 6 编译器构建 .NET 2.0 应用程序。
csc.exe /r:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll" /nostdlib Program.cs
您甚至可以在您的应用程序中使用 C# 6 功能! (只要它们是不涉及 .NET 运行时的仅编译器功能)
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"
}