6

最近、Visual Studio 11 Beta をインストールしました。既存の 4.0 プロジェクトを更新して 4.5 を使用しようとしています。プログラムでは、 を使用して動的に生成されたコードをコンパイルしますCSharpCodeProvider

/// <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 が見つかりません。

CompilerVersion4.5を使用するように指示する間違った方法を指定していますか? コードは新しい 4.5 固有の機能を使用しないため、v4.5 としてコンパイルしても違いはありますか?

4

1 に答える 1

12

問題を実証するための短いが完全なプログラムを提供していただければ助かりますが、VS11 を搭載したマシンで実行していると仮定して、「v4.0」で動作し、非同期メソッドをコンパイルできることを確認できます。ベータ版がインストールされました。

デモンストレーション:

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);
    }
}
于 2012-03-06T20:58:00.933 に答える