将 C# 项目编译为一个 dll
我有一个基础项目,可以满足两个不同的项目。我希望能够让主项目在编译时将所有引用编译到一个 dll 中。
这需要内置到项目构建中,这样初级开发人员就不必担心它。
解决方案:我最终在项目的构建后操作中添加了一行,以便良好的构建执行以下操作:
$(ProjectDir)ilmerge.exe /out: /ver: <.dll> <.dll>
ilmerge.exe
已移至项目,上面的行采用给定的 dll并创建一个。
I have a base project that feeds two different projects. I want to be able to have the primary project compile all the references into one dll at compile time.
This needs to be something that is built into the project build so that Jr developers don't have to worry about it.
Solution: I ended up adding a line to the Post-build action of the project, so that a good build does the following:
$(ProjectDir)ilmerge.exe /out: /ver: <.dll> <.dll>
ilmerge.exe
was moved to the project and the above line takes the dlls given and creates one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(此答案经过编辑,以更新新下载位置的链接并反映新的见解)
您最好的选择是使用 微软的 ILMerge。它曾经可以从 Microsoft Research 单独下载(它不是 VS 或 .NET SDK 的一部分)。
原始版本一直保留到 2012 年。现在是 NuGet 包,这是目前获取它的最佳方式,并且将其添加到您的项目并构建服务器。
作为替代方案,您可能需要尝试 CodePlex 中的 ILMerge GUI,它为 ILMerge 提供了 GUI 界面。它的开发曾被中断,但现在又重新活跃起来。
关于 ILMerge 的 Codeproject 文章 详细介绍了如何使用它并解释了一些用途-案件。示例命令如下:
作为替代方案,您可以将 Fody 与 Costura.Fody,如本 SO 响应。
注意(1):如果您需要合并并行程序集(即混合本机64位和32位程序集),您应该参考这篇关于加载的文章动态并排程序集。
注意 (2):要将其作为构建过程的一个组成部分添加到您的构建过程中,只需转到“项目属性”>“构建事件>单击“编辑构建后”并填写上面的命令。单击“宏”查看如何引用当前文件或程序集。
(this answer was edited to update links to new download locations and to reflect new insights)
Your best bet is to use Micorosoft's ILMerge. It used to be available as a separate download from Microsoft Research (it is not part of VS or the .NET SDK).
The original version was maintained until 2012. It is now a NuGet package, which is the best way to get it presently, and to add it to your project and build server.
As an alternative, you may want to try ILMerge GUI from CodePlex, which provides a GUI interface to ILMerge. Its development was interrupted, but is now active again.
A Codeproject article on ILMerge explains more on how to use it and explains some use-case. An example command is:
As an alternative, you can use Fody with Costura.Fody, as explained in this SO response.
Note (1): if you need to merge side-by-side assemblies (i.e., mix native 64 bit and 32 bit assemblies), you should consult this SO article on loading side-by-side assemblies dynamically.
Note (2): to add this as an integral step to your build process, just go to Project Properties > Build Events > click "Edit Post-build" and fill in the command from above. Click "Macros" to see how to reference the current file or assembly.
查看 Microsoft 的外部工具 ILMerge:http://research。 microsoft.com/en-us/people/mbarnett/ilmerge.aspx
它可以将多个程序集合并为一个程序集。
Have a look for the external tool ILMerge from Microsoft: http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx
It can merge multiple assemblies into just one.
您可以使用 ILMerge 来合并多个程序集:
您可以将基础程序集合并在一起,并将生成的程序集用作其他项目的引用。
或者,不要将两个项目放在一起,而是将共享代码放在一个项目中,以便引用单个程序集。
You can use ILMerge for merging multiple assemblies:
You could merge the base assemblies together and use the resultant assembly as a reference to your other projects.
Alternatively, instead of two projects, have your shared code placed in one project in order to get a single assembly to reference.