已部署的 .NET 程序如何引用 GAC 中的程序集?

发布于 2024-11-24 05:50:33 字数 235 浏览 6 评论 0原文

开发 .NET 程序的最佳实践似乎是这样的:开发时切勿引用 GAC 中的 dll。

但是,我应该如何让实际部署的产品引用 GAC 中的 dll?例如,如果我的产品是一个非常简单的 Windows 应用程序,那么我应该如何编写代码,以便单个部署的 .exe 文件引用来自 GAC 的 System.Windows.Forms.dll

我是 .NET 开发的新手,所以我可能会缺少一些非常基础的知识。

Best practice for developing .NET programs seems to be this: never reference dlls from GAC while developing.

However, How should I get the actual deployed product to reference the dlls in the GAC? For example, if my product is a very simple windows app, then how should I program my code so that the single deployed .exe file references System.Windows.Forms.dll from GAC?

I am new to .NET development, so I might be missing some very basics.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉 2024-12-01 05:50:34

简单的答案是不要引用 GAC 中的 System.Windows.Forms.dll。而是将该 DLL 包含在您的应用程序中。

更新

如果您希望在 GAC 中添加对 dll 的引用,您可以使用常用的“添加引用”对话框来执行此操作。

浏览到 C:\Windows\ assembly 可以选择这些引用。

The simple answer is not to reference System.Windows.Forms.dll from the GAC. Include that DLL in your application instead.

UPDATE

If you wish to add references to dlls in the GAC you can do so using the usual Add References Dialog.

Browsing to C:\Windows\assembly is where you can select those references.

欢烬 2024-12-01 05:50:33

您可以从添加引用对话框中的列表中添加引用。这会向您的 .csproj 添加如下所示的条目:

<ItemGroup>
  <Reference Include="System" />
  <Reference Include="System.Core" />
  <Reference Include="System.Xml.Linq" />
  <Reference Include="System.Data.DataSetExtensions" />
  <Reference Include="Microsoft.CSharp" />
  <Reference Include="System.Data" />
  <Reference Include="System.Xml" />
</ItemGroup>

您不会在项目中附带这些 dll。您可以提供 .net 框架的 redist 设置,但只有在您提供程序的物理副本(而不是提供下载)时,这才是一个好主意。将它们包含在您的程序中可能会侵犯版权。当然,在 Mono 上,未来的 windows/.net 版本等需要使用不同的 System.Windows.Forms 程序集,如果您分发自己的该文件副本,则该程序集将不起作用。

这些程序集将从 GAC 加载。但这没关系,因为它们毕竟是 .net 框架的一部分。编译后的引用将类似于 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089。这样,框架将选择正确的版本,并确保引用程序集来自正确的创建者。

不使用 GAC 的准则是指您自己的或第三方的程序集。您可以将它们与您的应用程序一起发送,并将它们放在与主程序相同的目录中。仅在少数特定情况下才需要将它们添加到 GAC。

You add the references from the list in the Add References dialog. This adds entries to your .csproj that look like the following:

<ItemGroup>
  <Reference Include="System" />
  <Reference Include="System.Core" />
  <Reference Include="System.Xml.Linq" />
  <Reference Include="System.Data.DataSetExtensions" />
  <Reference Include="Microsoft.CSharp" />
  <Reference Include="System.Data" />
  <Reference Include="System.Xml" />
</ItemGroup>

You don't ship these dlls with your project. You could ship the redist setup of the .net framework, but that's only a good idea if you ship physical copies of your program, not if you offer a download. Including them with your program is probably a copyright violation. And of course on Mono, future windows/.net versions etc a different System.Windows.Forms assembly needs to be used, that wouldn't work if you distributed your own copy of this file.

These assemblies will get loaded from the GAC. But that's fine since they are part of the .net framework after all. The compiled references will look similar to mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. This way the framework will choose the correct version and will make sure that the references assemblies are from the correct creator.

The guideline of not using the GAC refers to your own or third party assemblies. You ship those with your application and just have them in the same directory as your main program. Adding them to the GAC is only needed in a few specific scenarios.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文