C# 使用CSharpCodeProvider时在生成中嵌入依赖项

C# 使用CSharpCodeProvider时在生成中嵌入依赖项,c#,merge,compilation,.net-assembly,C#,Merge,Compilation,.net Assembly,我正在使用CSharpCodeProvider在程序运行时编译一个C#程序集,它依赖于一些构建为.dll的库,以及构建它的程序 理想情况下,我希望只构建一个可执行文件,而不必复制所有依赖项 下面是我用来编译程序集的代码: //Create the compiler (with arguments). CSharpCodeProvider provider = new CSharpCodeProvider(); CompilerParameters cp = new CompilerParamet

我正在使用
CSharpCodeProvider
在程序运行时编译一个C#程序集,它依赖于一些构建为
.dll
的库,以及构建它的程序

理想情况下,我希望只构建一个可执行文件,而不必复制所有依赖项

下面是我用来编译程序集的代码:

//Create the compiler (with arguments).
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = true;
cp.OutputAssembly = "example.exe";
cp.GenerateInMemory = false;

//Reference the main assembly (this one) when compiling.
Assembly entryasm = Assembly.GetEntryAssembly();
cp.ReferencedAssemblies.Add(entryasm.Location);

//Reference an external assembly it depends on.
cp.ReferencedAssemblies.Add("someExternal.dll");

//Attempt to compile.
CompilerResults results = provider.CompileAssemblyFromSource(cp, someScript);
这产生的可执行文件仍然需要
someExternal.dll
和运行时在同一目录中构建它的程序,我更希望它是一个包含所有依赖项的单一可执行文件


有什么方法可以做到这一点吗?

最后,我使用了
ILRepack.Lib
NuGet包来完成这项工作,它允许您以编程方式合并二进制文件,而无需使用命令行工具

下面是我用来将目录中的所有
.dll
文件打包到可执行文件中的代码:

//Setting required options.
RepackOptions opt = new RepackOptions();
opt.OutputFile = "example_packed.exe";
opt.SearchDirectories = new string[] { AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory };

//Setting input assemblies.
string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
opt.InputAssemblies = new string[] { "example.exe", entryasm.Location }.Concat(files).ToArray();

//Merging.
ILRepack pack = new ILRepack(opt);
pack.Repack();