我检查了有关roslyn的一些资源,但没有找到如何使用Roslyn将c#源代码编译为可执行文件。我可以使用CodeDom轻松地将某些.cs文件编译为.exe:
/// <summary>
/// "anycpu" || "anycpu32bitpreferred" || "x86" || "x64" || "ARM" || "Itanium"
/// </summary>
public static string param = "anycpu";
public static string BCS(string[] sources,string[] libs,string outPath,bool exef)
{
var options = new Dictionary<string, string> {
{ "CompilerVersion", "v4.0.0" }
};
CSharpCodeProvider codeProvider = new CSharpCodeProvider(options);
CompilerParameters parameters = new CompilerParameters(libs);
parameters.GenerateExecutable = exef;
parameters.OutputAssembly = outPath;
parameters.CompilerOptions = "-platform:" + param;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, sources);
if (results.Errors.Count > 0)
{
string errsText = "";
foreach (CompilerError CompErr in results.Errors)
{
errsText = "("+CompErr.ErrorNumber +
")Line " + CompErr.Line +
",Column "+CompErr.Column +
":"+CompErr.ErrorText + "" +
Environment.NewLine;
}
return errsText;
}
else
{
return "Success";
}
}
但是CodeDom存在问题-他只能使用.NET Framework 4.0编译c#,但是我需要使用4.6.1 .NET Framework版本来编译c#文件。因此,问题:我可以使用以下命令编译一些c#文件(.cs)吗? 4.6.1使用Roslyn编译器的.NET Framework版本?