在运行时编译程序集并将dll保存在一个文件夹中 [英] Compile assembly in runtime and save dll in a folder

查看:44
本文介绍了在运行时编译程序集并将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 :
");
    foreach (CompilerError error in results.Errors )
    {
        errors.AppendFormat("Line {0},{1}	: {2}
", 
               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.

推荐答案

您可以使用 CompilerParametersOutputAssembly 属性来设置输出程序集的名称(路径).从你的例子:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆