Using Global Assembly Cache (GAC) - in the way it was designed to

旧时模样 提交于 2019-12-13 12:21:58

问题


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.


回答1:


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)




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/3386142/using-global-assembly-cache-gac-in-the-way-it-was-designed-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!