将 dll 文件添加到 C# 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1029556/
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-06 05:55:30  来源:igfitidea点击:

Adding a dll file to a C# project

c#.netdllinstallationproject

提问by Mat

It's a beginners question, but...

这是一个初学者的问题,但是...

Image of dll reference and dll included in project file http://a3.vox.com/6a00c2251e5b66549d00e398ca81eb0003-pi

项目文件中包含的 dll 引用和 dll 的图像 http://a3.vox.com/6a00c2251e5b66549d00e398ca81eb0003-pi

If you look at the image above, there is the "Bass.Net" dll added as reference and also directly as file in the project.

如果你看上面的图片,有“Bass.Net”dll作为参考添加,也直接作为项目中的文件。

Can someone tell me what's the point of doing that?

有人能告诉我这样做有什么意义吗?

采纳答案by Otávio Décio

No reason, really. It could be that Visual Studio is set to display files not in the project (hard to tell from the picture) and the dll's happen to be in the main directory. The text is pretty clear that the extra files are

没有理由,真的。可能是 Visual Studio 设置为显示不在项目中的文件(从图片中很难分辨),而 dll 恰好在主目录中。文字很清楚,额外的文件是

  • bass.dll
  • bassenc.dll
  • lame.exe
  • 低音文件
  • 文件
  • 跛脚

The .net one happens to be with the others in the same directory and you need to add it as a reference.

.net 恰好与同一目录中的其他文件,您需要将其添加为参考。

回答by Groo

If an assembly (Bass.Net.dll in your case) contains classes you want to use, you must add a reference to that assembly to your project.

如果程序集(在您的情况下为 Bass.Net.dll)包含您要使用的类,您必须向您的项目添加对该程序集的引用。

回答by Hannoun Yassir

No point the best thing to do is get all your dependenicies and store them in a seperate folder and only reference them do not copy them to your solution ;)

最好的办法是获取所有依赖项并将它们存储在单独的文件夹中并且仅引用它们不要将它们复制到您的解决方案中;)

回答by adrianbanks

Having those in your project and output directory allows the final executing code to reference them without any issues running on different machines.

将它们放在您的项目和输出目录中允许最终执行代码引用它们,而不会在不同机器上运行时出现任何问题。

It sounds as it they put the reference dlls in the project directory, reference them from there, and also include them in the project. That way, when the project directory is copied, the reference dll will be copied with it. Additionally, if the reference dll is missing, the project will complain in Visual Studio.

听起来他们将引用 dll 放在项目目录中,从那里引用它们,并将它们包含在项目中。这样,当项目目录被复制时,引用 dll 将被复制。此外,如果缺少引用 dll,项目将在 Visual Studio 中报错。

回答by krohrbaugh

Within Windows, a DLL is a dynamic link library, which packages a set of programmatic functionality together. In this example, bass.dll exposes the features and functionality relevant to audio processing through this file (and any files it depends on). In order to use this functionality, you need the reference in the solution, so that Visual Studio can link itat compile time. The DLL will then typically be copied to your output directory when the application is built.

在 Windows 中,DLL 是一个动态链接库,它将一组编程功能打包在一起。在此示例中,bass.dll 通过此文件(以及它所依赖的任何文件)公开了与音频处理相关的特性和功能。为了使用此功能,您需要解决方案中的引用,以便 Visual Studio 可以在编译时链接它。DLL 通常会在构建应用程序时复制到您的输出目录。

That's all that is necessary to get the code to work properly, the rest is really just preference or convention. Some people prefer to have all the files that exist in the project directory in the solution, so that the Solution Explorer reflects the file system. Typically you will want to have libraries your application depends on somewhere in your solution directory hierarchy so that the entire application is packaged together (making source code control use easier, for instance). You won't want to put this library in the BIN directory or any directory that Visual Studio generates, though, to avoid accidental deletions. In any event, having the reference is the important part, the file being in the project or solution is not necessary.

这就是使代码正常工作所需的全部内容,其余的实际上只是偏好或约定。有些人喜欢在解决方案中包含项目目录中存在的所有文件,以便解决方案资源管理器反映文件系统。通常,您希望应用程序依赖于解决方案目录层次结构中的某个位置的库,以便将整个应用程序打包在一起(例如,使源代码控制更容易使用)。但是,您不希望将此库放在 BIN 目录或 Visual Studio 生成的任何目录中,以避免意外删除。无论如何,参考是重要的部分,项目或解决方案中的文件不是必需的。

Typically, you'll want to keep external libraries out of your source directories, though, so I wouldn't actually recommend this structure. I tend to use a structure like this, but, again, this is all preference:

通常,您会希望将外部库保留在源目录之外,因此我实际上不推荐这种结构。我倾向于使用这样的结构,但同样,这都是偏好:

  • Source: Source code and project files
  • Libraries: DLLs
  • Support: Miscellaneous code or projects, but not actually part of the application (perhaps deployment scripts)
  • 来源:源代码和项目文件
  • 库:DLL
  • 支持:杂项代码或项目,但实际上不是应用程序的一部分(可能是部署脚本)

回答by M.Turrini

It's really hard to guess why someone else did something, but if I really had to guess, I'ld say that the guy thought to embed the necessary dlls as resources to be sure it was availale to the application. I have seen this technique used to embed fonts or sounds and am not sure if it works at all with dlls; but it's just a guess.
Of course the best way to be sure the files were available would have been to create a deployment project, with Visual Studio or some other installation tool loke Wise or InnoSetup, just to name a few.

很难猜测为什么其他人会做某事,但如果我真的不得不猜测,我会说这个人认为将必要的 dll 作为资源嵌入,以确保它对应用程序可用。我已经看到这种技术用于嵌入字体或声音,但不确定它是否适用于 dll;但这只是一个猜测。
当然,确保文件可用的最佳方法是创建一个部署项目,使用 Visual Studio 或其他一些安装工具 loke Wise 或 InnoSetup,仅举几例。

回答by Justin Dearing

This actually might be a good idea in a lot of circumstances. In my opinion their are 3 types of dependencies

在很多情况下,这实际上可能是一个好主意。在我看来,它们是 3 种类型的依赖项

  1. Assemblies from the .Net standard library. Never include those locally.
  2. Assemblies that you expect other developers to install as part of an MSI or exe setup package. This usually means their strongly signed and have a copy in the GAC.
  3. Assemblies that you don't expect other developers to install via an MSI or exe installer. Maybe because you have a third party or in house library not in the GAC.
  1. .Net 标准库中的程序集。永远不要包括那些本地的。
  2. 您希望其他开发人员作为 MSI 或 exe 安装程序包的一部分安装的程序集。这通常意味着他们在 GAC 中有强有力的签名和副本。
  3. 您不希望其他开发人员通过 MSI 或 exe 安装程序安装的程序集。可能是因为您拥有不在 GAC 中的第三方或内部图书馆。

In the third case, the simplest thing to do is store a copy of the DLL in the source repo.

在第三种情况下,最简单的做法是在源代码库中存储 DLL 的副本。