Bra*_*AGr 6 c# compiler-construction .net-4.5 csharpcodeprovider
我最近安装了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;
}
}
Run Code Online (Sandbox Code Playgroud)
当我将CompilerVersionfrom 更改为时,问题就出现{ "CompilerVersion", "v4.0" }了{ "CompilerVersion", "v4.5" }
我现在得到一个例外
无法找到编译器可执行文件csc.exe.
指定CompilerVersion错误的方法告诉它使用4.5?将其编译为v4.5甚至会有所不同,因为代码将不会使用任何新的4.5特定功能?
Jon*_*eet 11
它会帮助,如果你想给我们一个简短而完整的程序来说明这个问题,但是我可以确认为"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);
}
}
Run Code Online (Sandbox Code Playgroud)