使用全局程序集缓存 (GAC) - 按照其设计的方式

发布于 2024-09-12 14:16:30 字数 580 浏览 1 评论 0原文

以下解决方案是在代码中使用 GAC 库的唯一可能性吗?

Assembly lib = Assembly.Load("MyLibrary, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31f5625abd53197f");

Console.WriteLine(lib.GetType("MyClass").GetMethod("Start").Invoke(obj, null));

我有点困惑 - 我已经阅读了很多有关 GAC 的内容,并且知道如何在 GAC 中签署程序集、安装和卸载程序集,但不知道如何使用它,以及它如何帮助程序员(除了它)安全地存储同一库的不同版本)。我希望我可以正常创建类,而不是被迫调用上面提供的方法。

我不想要任何解决方法,例如:“更改 Windows 注册表”,因为我不认为 GAC 是为此类操作而设计的。我想要一个简单的答案:GAC 的用途是什么?运行时环境是否以某种方式使用它?

当代码变得非常丑陋且难以管理时,使用 GAC 的意义何在?或者也许我错过了什么?也许我应该手动将程序集复制到本地文件夹中?但我听说这也很难做到。

Is the following solution the only possibility to use libraries from GAC in code?

Assembly lib = Assembly.Load("MyLibrary, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31f5625abd53197f");

Console.WriteLine(lib.GetType("MyClass").GetMethod("Start").Invoke(obj, null));

I am a little bit confused - I've read quite much about GAC and I know how to sign an assembly, intall and uninstall assembly in GAC, but have no idea how to use it, and how does it help programmers (except that it stores different versions of same library safely). I wish I could normally create classes, not beeing forced to invoke methods presented above.

I don't want any work-arounds such as: "change windows registry" because I don't think GAC was designed for such manipulations. I want a simple answer: what the GAC is for, does runtime environment use it somehow?

What is the point of using GAC, when the code gets really ugly and difficult to manage? Or maybe I am missing something? Maybe I should manually copy an assembly into my local folder? But I heard it's also hard to do.

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

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

发布评论

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

评论(3

顾北清歌寒 2024-09-19 14:16:30

GAC 可以帮助您并排安装程序集的多个版本(这就是公钥的用途 - 防止名称冲突)整个机器,而不仅仅是您的应用程序目录。

所以,是的,使用版本号从 GAC 访问程序集几乎正是 GAC 的设计方式。 ;-)

顺便说一句:您不应该在不必要时动态加载内容。

IOW:如果您已经知道类名称和程序集版本,则可以仅引用该程序集并跳过不仅动态加载程序集而且还通过反射调用方法的相当严重的性能损失。 (而不是通过接口或委托重用它们)

The GAC is there to help you have multiple versions of your (that's what the public key is for - preventing name clashes) assemblies installed side-by-side, and available to the whole machine, not just your application directory.

So yes, accessing assemblies from the GAC using the version number is pretty much exactly the way the GAC was designed for. ;-)

btw: You should not dynamically load stuff when you don't have to.

IOW: If you already know the class name and assembly version, you could just reference that assembly and skip the rather dramatic performance penalty of not only loading an assembly dynamically, but also invoking methods through reflection. (Instead of e.g. reusing them via interfaces or delegates)

冰魂雪魄 2024-09-19 14:16:30

如果您的程序集已被 ngen'ed 或 GAC'd,那么如果所有内容(哈希值等)都匹配,应用程序将自动使用它。

If your assembly has been ngen'ed or GAC'd, then the application will use that automatically, if everything (hashes, etc) matches.

活雷疯 2024-09-19 14:16:30

如果您在项目中引用该程序集,则可以像正常引用不在 GAC 中的 DLL 一样使用它。

If you reference the assembly in your project, you can use it like a normally referenced DLL that is not in the GAC.

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