views:

99

answers:

3

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  A: 

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

leppie
A: 

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

ck
+2  A: 

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)

Robert Giesecke
Then why it is said that GAC helped to solve the so called "DLL Hell" problem, if there are only my libraries? And what is the point to put my assemblies there if I don't know how to get them out and reuse? I still need to have a local copy :/. If I understand correctly, you advised me to search manually the file in my Windows folder. Is the use-through-reflection-way ever used in apliaction? And one more: how is mscorlib assembly linked to my applications, since it resides in GAC, but I use it normally (not through reflection)? (sorry, I just want to understand it ;/)
MarcAndreson
DLL Hell consists of globally installed libraries that get updated and these updates sometimes break other apps requiring another version of library xyz. Also, classic DLLs where only accessed by their filename, while the GAC also uses the version and the public key which is generated from your strong name. So even a library with the same name will not clash with yours, given that there public key differs.-- Why do you think, you need a local copy when you installed your assemblies into the GAC?
Robert Giesecke
@Marc, simply use the Add Reference window to add a reference to an assembly you've installed in the GAC, and you can use its types like any other referenced assembly, without specifying anything in code (just like you use various System.* classes).
Allon Guralnek
@Robert why local copy? because otherwise I would need to use reflection in order to reuse my assembly, wouldn't I ?
MarcAndreson
@Marc, no, you wouldn't. It seems like you are confusing deployment with development. During development, it can make perfect sense to reference a physical file, most of the time the IDE does it for you when ou reference one project from another. For the deployment of your application, it **can** make sense to install assemblies into the GAC and then, you do **not** have to also put it into your application directory as well.
Robert Giesecke