c# - 在运行时编译程序集并将dll保存在文件夹中

标签 c# compilation runtime .net-assembly

这是我的代码:

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/”文件夹中。

最佳答案

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

...
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateInMemory = false;

compilerparams.OutputAssembly = "OutputAssembly.dll";

CompilerResults results = compiler.CompileAssemblyFromSource(compilerparams, code);
...

关于c# - 在运行时编译程序集并将dll保存在文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21013572/

相关文章:

c# - 如何缩小 .net AXD 资源

c# - 在设置和部署中保护 dll (visual studio 2008)

当被询问时,C 在运行时将数组初始化和重新初始化为零

algorithm - {0,1} 上的序列数,使得序列至少包含一半的

c# - Unity C#在运行时添加歌曲,然后播放该歌曲

c# - 遗留代码 visual basic to c#

c# - Rider 中对托管代码规则集的支持

Java和android测试方法

android - 用于 Android 工具链的 FFMPEG : arm-linux-armeabi-eabi-pkg-config - is there any toolchain in existence containing the pkg-config tool

android - 如何从已编译的二进制文件 (.so) 中删除字符串