在 C# 中静态链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:47:02  来源:igfitidea点击:

Linking statically in C#

提问by erlando

I'm working on a module for a CMS. This module is distributed as a class library DLL.

我正在为 CMS 开发一个模块。此模块作为类库 DLL 分发。

I have several utility libraries I'd like to use in this module. Is there anyway I can link these libraries statically so I won't have to distribute several DLL's (thereby distributing my utility libraries separately)?

我想在这个模块中使用几个实用程序库。无论如何我可以静态链接这些库,这样我就不必分发几个 DLL(从而分别分发我的实用程序库)?

I would like to have only one DLL.

我只想拥有一个 DLL。

采纳答案by Seb Nilsson

You can merge your many DLLs with ILMERGE:

您可以使用 ILMERGE 合并许多 DLL:

http://research.microsoft.com/~mbarnett/ILMerge.aspx

http://research.microsoft.com/~mbarnett/ILMerge.aspx

Haven't tried it myself. Hope it helps.

自己没试过。希望能帮助到你。



Download here:
http://www.microsoft.com/downloads/details.aspx?familyid=22914587-B4AD-4EAE-87CF-B14AE6A939B0&displaylang=en

在这里下载:http: //www.microsoft.com/downloads/details.aspx?familyid=22914587-B4AD-4EAE-87CF-B14AE6A939B0&
displaylang=en

Brief Description(from download-page)
ILMerge is a utility for merging multiple .NET assemblies into a single .NET assembly. It works on executables and DLLs alike and comes with several options for controlling the processing and format of the output. See the accompanying documentation for details.

简要说明(来自下载页面)
ILMerge 是用于将多个 .NET 程序集合并为单个 .NET 程序集的实用程序。它适用于可执行文件和 DLL,并带有几个用于控制输出处理和格式的选项。有关详细信息,请参阅随附的文档。

回答by khebbie

The short answer for this is no! You can not link in a dll during compilation. I don't know if there is some subtle way to do this, but you would probably have to distribute the dlls along with your cms. The best way to do this is to make some kind of re-distributable.

对此的简短回答是否定的!您不能在编译期间链接 dll。我不知道是否有一些微妙的方法可以做到这一点,但您可能必须将 dll 与您的 cms 一起分发。做到这一点的最好方法是使某种可重新分发。

回答by Kaganar

If you don't want to use ILMerge, see this page:

如果您不想使用 ILMerge,请参阅此页面:

http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx

http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx

editor's note: Jeffrey Richter advices to put your dlls into exe file as resources (For each DLL file you add, display its properties and change its “Build Action” to “Embedded Resource.”). Then a custom class loader is needed to make the executable work (At runtime, the CLR won't be able to find the dependent DLL assemblies, which is a problem. To fix this, when your application initializes, register a callback method with the AppDomain's ResolveAssembly event).

编者注:Jeffrey Richter 建议将您的 dll 作为资源放入 exe 文件中(对于您添加的每个 DLL 文件,显示其属性并将其“构建操作”更改为“嵌入式资源”。)。然后需要一个自定义的类加载器来使可执行文件工作(在运行时,CLR 将无法找到依赖的 DLL 程序集,这是一个问题。要解决这个问题,当您的应用程序初始化时,使用AppDomain 的 ResolveAssembly 事件)。

Be sure to change the resourceNamestring to point to your actual resources. (e.g. change AssemblyLoadingAndReflectionto your project name.)

请务必更改resourceName字符串以指向您的实际资源。(例如更改AssemblyLoadingAndReflection为您的项目名称。)