简体   繁体   English

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

[英]How do deployed .NET programs reference assemblies in GAC?

Best practice for developing .NET programs seems to be this: never reference dlls from GAC while developing.开发 .NET 程序的最佳实践似乎是这样的:开发时永远不要引用 GAC 的 dll。

However, How should I get the actual deployed product to reference the dlls in the GAC?但是,我应该如何让实际部署的产品引用 GAC 中的 dll? 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? 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.我是 .NET 开发的新手,所以我可能会遗漏一些非常基础的知识。

You add the references from the list in the Add References dialog.您可以从“添加参考”对话框中的列表中Add References This adds entries to your .csproj that look like the following:这会将条目添加到您的.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>

You don't ship these dlls with your project.您不会将这些 dll 与您的项目一起提供。 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.您可以发布 .net 框架的 redist 设置,但这只有在您发布程序的物理副本时才是一个好主意,而不是在您提供下载的情况下。 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.当然,在 Mono、未来的 windows/.net 版本等不同的System.Windows.Forms程序集需要使用,如果你分发你自己的文件副本,那将不起作用。

These assemblies will get loaded from the GAC.这些程序集将从 GAC 加载。 But that's fine since they are part of the .net framework after all.但这很好,因为它们毕竟是 .net 框架的一部分。 The compiled references will look similar to mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 .编译后的引用类似于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.不使用 GAC 的指南是指您自己或第三方程序集。 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.仅在少数特定情况下需要将它们添加到 GAC。

The simple answer is not to reference System.Windows.Forms.dll from the GAC.简单的答案是不要参考 GAC 的System.Windows.Forms.dll Include that DLL in your application instead.将 DLL 包含在您的应用程序中。

UPDATE更新

If you wish to add references to dlls in the GAC you can do so using the usual Add References Dialog.如果您希望在 GAC 中添加对 dll 的引用,您可以使用通常的添加引用对话框来完成。

Browsing to C:\Windows\assembly is where you can select those references.浏览到C:\Windows\assembly是您可以 select 那些参考的地方。

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

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