简体   繁体   中英

.Net Core - Are dependencies resolved on assembly loading?

Imagine that in your library A, you declared a dependency to library B that you will never use.

If you try to dynamic load A and B is not around, will your application crash? or, as long as the methods that you call don't try to reach code on B you're in the clear?

This depends on what you mean by "never use."

  1. The code used to compile Library A never uses any API that is defined by library B.
  2. The code in Library A uses API that is defined by Library B, but the code path in which that API is used is never called at runtime.

In case 1, the compiler will trim the unused references when it produces libraryA.dll. You don't need libraryB.dll and the runtime won't try to load it.

In case 2, you might be ok, but there are no guarantees. For example, if you had a class that references libraryB, even it that reference is from a method you never call, the runtime may still try to load libraryB.dll. You can sometimes cleverly structure code to avoid this load, but you're playing with fire. It is best to have libraryB available.

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