4
votes

Normally a referenced assembly of an assembly is loaded when the first method from a type in the referenced assembly is executed.

Does it make sense to force loading all referenced assemblies at a point where the application flow can tolerate a delay to avoid it in further execution where it might not be tolerable (e.g. in a time critical method)?

If yes, what's the best way to do that? (Reflection, ...)

1
By far the best way is to execute it once when time is not critical. That ensures the assembly file is found on the disk, its IL got loaded and the code just-in-time compiled. Beware the garbage collector, it may cause pauses so write the method to only use pre-allocated objects.Hans Passant
Ok, can you be more specific what has to be executed once when time is not critical?Harry13
Thanks, now I know what you mean...Harry13

1 Answers

2
votes

One of my present employer's products gets a list of all the DLLs from the directory of the entry assembly. It then loads them all using Assembly.LoadFrom. It does this while the splash screen is up. Frankly, the code scares me. We've had to put in some hacks to avoid certain DLLs. We've had to change the installer to wipe the target directory clean before updating. It's a very insecure plan.

At a previous job, I wrote a similar function that used the GetReferencedAssemblies method. Starting with the entry assembly, it would recursively call that followed by Assembly.LoadFrom. It would stop the recursion after it loaded an assembly that was not shipped with our product. It worked, but I have since decided it was unnecessary.

In the present product I work on, we use Autofac to build the full dependency tree for the application. The bootstrapper code to configure that references all the services in the entire project -- I would guess that's at least 70% of the code. Again, this is triggered while the splash screen is up. This is the right approach. It balances "loading the necessities" with "taking time to load everything including stuff that may never be used".