Fast dynamic loading of assemblies in C#.

Hi,
I want to dynamically load some assemblies and invoke certain functions given in those assemblies. All these functions will implement the same interface, so that I don't have to bear the performance hit of a Type.InvokeMember call. For loading these assemblies dynamically I will, obviously, have to use the Assembly.Load/LoadFrom routines.
My question is, is it advisable to put some assemblies (which may be dynamically loaded frequently) into the GAC or have some sort of a replacement algorithm (like LRU) to add and remove these assemblies from the GAC Also, if I put these assemblies into the GAC, will the Assembly.Load/LoadFrom routines check the GAC first while searching for the assembly or will I have to query the GAC to find whether the requested assembly exists in the GAC or not and then proceed

Thanks for any thoughts or help.
Cheers


Answer this question

Fast dynamic loading of assemblies in C#.

  • cngzhnmrl

    No, it does not make sense to put those in the GAC, except if you would not know where to find the DLLs. Assembly load would try to load from the assembly of course, but you wont gain any performance from that and a replacement would not work. Once the assembly is loaded in your application, it is known and wont be reloaded, so even if you exchange the assembly in the GAC afterwards, your application would ignore that until it is restarted (or the application domain is destroyed).
  • SaurabhJain

    Ok, so I can just keep on dynamically loading the assemblies and hope for the GC to do the cleaning up later

    Thanks for the reply :)

  • cssjm

    One does not really have to do with the other. If you dynamically load an assembly and instantiate a class from the assembly, the type of the class and the assembly is known to your application - that will not change until your application shuts down. If you lose/eleminate any reference to the instantiated class then the garbage collector will pick up that class instance of course, but the class and the assembly are still known to your application.

    I don't know what your intentions are with the dynamic loading, but if you plan to do something like having an application that runs 24/7 and you just exchange the DLLs it loads during runtime so that it picks up new functionality, then it will not work this way. Because as said, once a class and assembly is known it will not be reloaded, furthermore the DLL-file (if not in the GAC) will be locked (in use by your application) and cannot be replaced. If you plan to do something like this, take a look at this thread here: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=350139&SiteID=1


  • Fast dynamic loading of assemblies in C#.