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

时间:2022-03-02 03:52:11

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

以下解决方案是否是在代码中使用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));

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.

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

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?

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

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.

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

3 个解决方案

#1


4  

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

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

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

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

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)

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

#2


1  

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

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

#3


0  

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

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

#1


4  

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

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

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

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

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)

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

#2


1  

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

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

#3


0  

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

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