2012-03-06 104 views
5

我最近安裝了Visual Studio 11測試版,並試圖更新現有的4.0項目以使用4.5。在程序中,它使用CSharpCodeProvider編譯一些動態生成的代碼。使用CSharpCodeProvider與.net 4.5測試版

/// <summary> 
/// Compile and return a reference to the compiled assembly 
/// </summary> 
private Assembly Compile() 
{ 
    var referencedDlls = new List<string> 
    { 
     "mscorlib.dll", 
     "System.dll", 
     "System.Core.dll", 
    }; 
    referencedDlls.AddRange(RequiredReferences); 

    var parameters = new CompilerParameters(
     assemblyNames: referencedDlls.ToArray(), 
     outputName: GeneratedDllFileName, 
     // only include debug information if we are currently debugging 
     includeDebugInformation: Debugger.IsAttached); 
    parameters.TreatWarningsAsErrors = false; 
    parameters.WarningLevel = 0; 
    parameters.GenerateExecutable = false; 
    parameters.GenerateInMemory = false; 
    parameters.CompilerOptions = "/optimize+ /platform:x64"; 

    string[] files = Directory.GetFiles(GenerationDirectory, "*.cs"); 

    var compiler = new CSharpCodeProvider(
     new Dictionary<string, string> { { "CompilerVersion", "v4.5" } }); 
    var results = compiler.CompileAssemblyFromFile(parameters, files); 

    if (results.Errors.HasErrors) 
    { 
     string firstError = 
      string.Format("Compile error: {0}", results.Errors[0].ToString()); 
     throw new ApplicationException(firstError); 
    } 
    else 
    { 
     return results.CompiledAssembly; 
    } 
} 

問題是當我改變了CompilerVersion{ "CompilerVersion", "v4.0" }{ "CompilerVersion", "v4.5" }

我現在得到一個異常

編譯器可執行文件CSC.EXE無法找到。

是否指定CompilerVersion錯誤的方式告訴它使用4.5?將它編譯爲v4.5甚至會有所作爲,因爲代碼不會使用任何新的4.5特定功能?

+2

我不知道這是錯誤的指定編譯器的版本一樣,但FWIW在.NET 4.5編譯器仍然是根據輸出4.0版本。 – 2012-03-06 20:48:39

+0

爲什麼你只返回第一個錯誤? – abatishchev 2012-03-07 17:28:48

+0

@abatishchev由於我正在編譯的代碼都是自動生成的,因此通常不應該有任何錯誤,除非模板混亂了,只是返回第一個阻止它返回可能的數千個錯誤,這隻會導致一個非常長的錯誤電子郵件,而不是真的有幫助 – BrandonAGr 2012-03-07 17:45:10

回答

9

如果你給我們一個簡短但完整的程序來演示這個問題會有幫助,但是我可以用「v4.0」驗證它會工作並編譯異步方法,假設你運行在安裝了VS11 beta的機器。

示範:

using System; 
using System.Collections; 
using System.Reflection; 
using System.Collections.Generic; 
using System.Diagnostics; 
using Microsoft.CSharp; 
using System.CodeDom.Compiler; 

namespace Foo {} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var referencedDll = new List<string> 
     { 
      "mscorlib.dll", 
      "System.dll", 
      "System.Core.dll", 
     }; 

     var parameters = new CompilerParameters(
      assemblyNames: referencedDll.ToArray(), 
      outputName: "foo.dll", 
      includeDebugInformation: false) 
     { 
      TreatWarningsAsErrors = true, 
      // We don't want to be warned that we have no await! 
      WarningLevel = 0, 
      GenerateExecutable = false, 
      GenerateInMemory = true 
     }; 

     var source = new[] { "class Test { static async void Foo() {}}" }; 

     var options = new Dictionary<string, string> { 
      { "CompilerVersion", "v4.0" } 
     }; 
     var compiler = new CSharpCodeProvider(options); 
     var results = compiler.CompileAssemblyFromSource(parameters, source); 

     Console.WriteLine("Success? {0}", !results.Errors.HasErrors); 
    } 
} 
+1

它仍然是4.0版本,因爲4.5仍處於測試階段? CompilerVersion會在最終版本中被碰撞嗎?另外,CompilerVersion是框架版本還是C#編譯器版本? – BFree 2012-03-06 21:16:36

+0

謝謝,它看起來像4.5安裝在4的頂部,並具有相同的版本號我不需要更改CompilerVersion以使用4.5 – BrandonAGr 2012-03-06 21:26:45

+0

@BFree:不想說,說實話。 – 2012-03-06 21:31:42