2012-03-06 13 views
5

私は最近Visual Studio 11 Betaをインストールしましたが、4.5を使用するように既存の4.0プロジェクトを更新しようとしています。プログラムでは、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", "v4.5" }

{ "CompilerVersion", "v4.0" }からCompilerVersionを変更したときに、私は今

コンパイラの実行可能ファイルcsc.exeが見つからない例外を取得しています。

CompilerVersionを指定すると間違った方法で4.5を使用するように指定されていますか?コードが新しい4.5固有の機能を使用しないので、それをv4.5としてコンパイルすると違いが生じますか?

+2

私はそのようなコンパイラのバージョンを指定することが間違っている場合は知っているが、FWIWません.NET 4.5でのコンパイラは、出力に応じて、まだバージョン4.0です。 –

+0

なぜ最初のエラーのみを返すのですか? – abatishchev

+0

@abatishchev私がコンパイルしているコードはすべて自動生成されているので、テンプレートが乱れない限り、通常はエラーが発生しません。最初のコードを返すだけで何千ものエラーが返されることを防ぎます。電子メールでもう役に立ちません。 – BrandonAGr

答えて

9

問題を示すための短くて完全なプログラムを与えてもらえれば助けになりましたが、 "v4.0"で動作すると非同期メソッドをコンパイルすることができます。 VS11ベータ版がインストールされたマシン。

デモンストレーション:

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.5はまだベータ版なので、まだバージョン4.0ですか?コンパイラのバージョンは最終版でバンプされますか?また、CompilerVersionはフレームワークバージョンかC#コンパイラバージョンですか? – BFree

+0

ありがとう、それは4の上に4.5がインストールされ、同じバージョン番号を持っているように見えます4.5を使用するためにCompilerVersionを変更する必要はありません。 – BrandonAGr

+0

@BFree:正直言って言いたくありません。 –

関連する問題