我之前将 CodeDOM CompilationUnits 导出到文件,然后将这些文件读回以使用 CSharpCodeProvider 编译它们。今天我重构了代码,以便将 CodeDOM 导出为字符串:
public static string compileToString(CodeCompileUnit cu){
// Generate the code with the C# code provider.
CSharpCodeProvider provider = new CSharpCodeProvider();
using (StringWriter sw = new StringWriter())
{
IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
// Generate source code using the code provider.
provider.GenerateCodeFromCompileUnit(cu, tw,
new CodeGeneratorOptions());
tw.Close();
return sw.ToString ();
}
}
然后更改编译,使其使用 CompileFromSource:
public static Assembly BuildAssemblyFromString(string code){
Microsoft.CSharp.CSharpCodeProvider provider =
new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateExecutable = false;
compilerparams.GenerateInMemory = true;
compilerparams.CompilerOptions = "/nowarn:162";
string[] files = new string[]{"TemplateAesthetic.cs"};
CompilerResults results =
compiler.CompileAssemblyFromSource(compilerparams, code);
if (results.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
foreach (CompilerError error in results.Errors )
{
errors.AppendFormat("Line {0},{1}\t: {2}\n",
error.Line, error.Column, error.ErrorText);
Debug.Log (error.ErrorText);
}
}
else
{
return results.CompiledAssembly;
}
return null;
}
感谢 Maarten 注意到:问题是我需要在编译过程中包含一个真实文件(TemplateAesthetic.cs),但这个编译是从一个字符串发生的。您可以使用 CompileAssemblyFromSource 以这种方式进行混合编译吗?