如何使用C#6.0编译.NET 2.0? [英] How do I compile for .NET 2.0 with C# 6.0?

查看:137
本文介绍了如何使用C#6.0编译.NET 2.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Visual Studio 2015 has no problem compiling for the older CLR with new c# compiler. It seems that it uses VBCSCompiler.exe for this under the hood, but I cannot find any documentation about VBCSCompiler.exe command line options.

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

On the other hand csc.exe does not seem to have an option to select target CLR. You can use the latest csc.exe which will compile for CLR 4, or you can use an older csc.exe to compile for CLR 2, but then it won't be C# 6.

那么如何编译CLR 2和c#6.0呢?我必须有视觉工作室吗?

So how do I compile for CLR 2 and c# 6.0? Do I have to have visual studio for this? Are there any other options?

推荐答案

您可以使用 / r

 /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)

需要抑制自动包含现代mscorlib与 / nostdlib

You will also need to suppress the automatic inclusion of the modern mscorlib with /nostdlib:

 /nostdlib[+|-]                Do not reference standard library (mscorlib.dll)

使得它可以使用C#6编译器构建.NET 2.0应用程序。

Together, these make it so that you can build .NET 2.0 apps with the C# 6 compiler.

csc.exe /r:"C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll" /nostdlib Program.cs

你甚至可以在你的应用程序中使用C#6功能! (只要它们是纯编译器特性,不涉及.NET运行时)

You can even use C# 6 features in your app! (as long as they are compiler-only features that don't involve the .NET runtime)

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"
}

这篇关于如何使用C#6.0编译.NET 2.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆