在运行时编译程序集并将dll保存在文件夹中 [英] Compile assembly in runtime and save dll in a folder
本文介绍了在运行时编译程序集并将dll保存在文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
Microsoft.CSharp.CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateInMemory = false;
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);
}
throw new Exception(errors.ToString());
}
else
{
return results.CompiledAssembly;
}
如何将创建的dll保存到我自己的特定文件夹?当我调试时,不知何故装配位置在'AppData / Local / Temp /'文件夹。
How do I save the created dll to my own specific folder? When I debug, somehow the assembly location is at 'AppData/Local/Temp/' folder.
推荐答案
code> OutputAssembly 属性 CompilerParameters
设置输出程序集的名称(路径)。
从您的示例:
You could use the OutputAssembly
property of CompilerParameters
to sets the name (path) of the output assembly.
From your example:
...
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateInMemory = false;
compilerparams.OutputAssembly = "OutputAssembly.dll";
CompilerResults results = compiler.CompileAssemblyFromSource(compilerparams, code);
...
这篇关于在运行时编译程序集并将dll保存在文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文