4

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, ...)

3
  • 2
    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. Commented Oct 30, 2012 at 10:58
  • Ok, can you be more specific what has to be executed once when time is not critical?
    – Harry13
    Commented Oct 30, 2012 at 12:29
  • Thanks, now I know what you mean...
    – Harry13
    Commented Oct 30, 2012 at 14:23

1 Answer 1

2

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.