Reputation: 743
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, ...)
Upvotes: 4
Views: 275
Reputation: 5414
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".
Upvotes: 2