简体   繁体   中英

How are assemblies loaded into the AppDomain?

What determines when and if an assembly gets loaded into the AppDomain.

I guess I am trying to understand if:

  1. I am guaranteed that all project referenced assemblies will be loaded at the start of my application.
  2. If not when exactly do they get loaded? Is it going to be the first time I use a class/function from that assembly?
  3. Is there a way to tell at compile time if an assembly must be loaded first, before execution of the first line in my code? (I know I can use reflection, but I want to know if I can configure this at compile time)
  4. Assuming I can control when an assembly gets loaded, what about the dependent assembly? Can I tell the execution to only load up to 1st/2nd degree and let the rest be loaded dynamically?

I don't know the details but I know that there is event thorwn when an assemblies can't be loaded. So I guess they are loaded at runtime and you could add routines to it.

I think this will help you a bit about Assembly Resolve http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx

And this about Assembly Load Events http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyload.aspx

  1. no, you are guaranteed the opposite: only assemblies directly needed at start are loaded.
  2. yes, there will be attempt to load assembly as soon as some class will require type information from that assembly or code that needs that assembly will be JITed.
  3. no, there is no way at compile time to force assembly load sequence short of referencing something from each assembly in your Main (note that usually goal is opposite - to delay loading of as many assemblies as possible to speed up loading of an app).
  4. no, you can't control automatic loading (as Simon Edström points out there Assembly Resolve event fired when CLR decides it needs an assembly). You can always pre-load assemblies yourself if you know dependencies.

Note: assemblies don't "depend" on each other directly, just classes/methods in each depend on each other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM