简体   繁体   English

如何使.NET类库可链接(没有DLL)

[英]How to make .NET class library linkable (no DLL)

In the "old days" of C++, you had two options for creating a (class or otherwise) library - DLL or static linkable. 在C ++的“旧时代”中,您有两种选择来创建(类或其他)库 - DLL或静态可链接。

I have some projects in my solution that are "shared" libraries to be used by two or more other projects in the same solution. 我的解决方案中有一些项目是“共享”库,可供同一解决方案中的两个或多个其他项目使用。 Right now, they are being exported as DLLs which are then faithfully copied to the 'bin' folder of every single one of the actual executables that are using them. 现在,它们被导出为DLL,然后忠实地复制到使用它们的每个实际可执行文件的“bin”文件夹中。

I'm tired of seeing all of these DLLs running around (for both my libraries and Nuget-provided third-party libraries) and I would actually prefer to "link them in" to the executables, if there is a .NET way of doing so (short of just putting the code in separate folders and including them in all of the projects, which is kind of ugly with respect to making it obvious what is going on in by inspection of the Solution Explorer). 我已经厌倦了看到所有这些DLL运行(对于我的库和Nuget提供的第三方库)我真的更喜欢将它们“链接”到可执行文件中,如果有.NET方法的话所以(只是将代码放在单独的文件夹中,并将它们包含在所有项目中,这对于通过检查解决方案资源管理器显而易见的事情来说是很难看的)。

So - is "linking" libraries dead and gone with .NET? 那么 - “连接”库是否已经死亡并且与.NET一起消失了? If so, is there at least a way of registering assemblies (the way we used to register COM DLLs in the past) so there is only a single copy of the DLL in cases where more than one of the executables in the solution is installed instead of having the same DLLs replicated in multiple places? 如果是这样,至少有一种注册程序集的方式(我们以前用来注册COM DLL的方式),所以在安装解决方案中有多个可执行文件的情况下,只有DLL的一个副本在多个地方复制相同的DLL?

AFAIK .NET (and the CLR) don't have the notion of statically linked libraries. AFAIK .NET(和CLR)没有静态链接库的概念。

As SLaKs points out, there are tools to fool with the IL, but you'd need to weigh the cost of this added complexity to the benefit of simply reducing the number of DLL files floating around; 正如SLaKs指出的那样,有一些工具可以愚弄IL,但你需要权衡这种增加的复杂性的成本,以减少浮动的DLL文件的数量; is it really that important? 真的那么重要吗?

Instead, you can use a "Shared Project", at least since Visual Studio 2015. 相反,您可以使用“共享项目”,至少从Visual Studio 2015开始。

共享项目

It simply acts as a reference-able container for common code/resources, which is pretty close to the result you're looking to achieve. 它只是作为公共代码/资源的可引用容器,它非常接近您希望实现的结果。

See " What is the difference between a Shared Project and a Class Library in Visual Studio 2015? ". 请参阅“ Visual Studio 2015中共享项目和类库之间的区别是什么? ”。

As per SLaKs, also see " Static Linking of libraries created on C# .NET ". 根据SLaKs,还可以看到“ 在C#.NET上创建的库的静态链接 ”。

Actually, I discovered a "work-around" that gives me much the effect I want. 实际上,我发现了一种“解决方法”,它给了我很多我想要的效果。 The Costura.Fody Nuget package collects everything necessary (including all the DLLs that are normally copied into the bin/[configuration] folder) into resources in your final executable at compile-time. Costura.Fody Nuget包在编译时将所有必要的(包括通常复制到bin / [configuration]文件夹中的所有DLL)收集到最终可执行文件中的资源中。

I'm going to mark another answer, as it is spot on point for my original question; 我将标记另一个答案,因为它是我原始问题的重点; however, if anyone else comes looking, they may want to consider the Costura.Fody package if it meets what they want to accomplish. 然而,如果有其他人来看,他们可能想要考虑Costura.Fody包,如果它满足他们想要完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM