2016-04-06 49 views
3

對於使用新的c#編譯器編譯舊版CLR,Visual Studio 2015沒有任何問題。它看起來它使用VBCSCompiler.exe在這個底層,但我找不到有關VBCSCompiler.exe命令行選項的任何文檔。如何使用C#6.0編譯.NET 2.0?

在另一方面CSC.EXE似乎並不有一個選項,選擇目標CLR。您可以使用最新的csc.exe,它將爲CLR 4編譯,或者您可以使用較舊的csc.exe編譯爲CLR 2,但不會是C#6.

那麼如何編譯CLR 2和C#6.0?我必須爲此擁有視覺工作室嗎?還有其他選擇嗎?

+0

我只是對不再支持2.0的強制性警告發表評論。 –

回答

6

您可以/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) 

您還需要壓制自動加上現代的mscorlib與/nostdlib

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