Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly.LoadFile look dependencies in Location of Executeable

I wrote a wrapper class which loads other assemblies using reflection. Wrapper.dll is also being loaded by a process through reflection following is the diagram to explain the scenario.

Process.exe  
         |___Loads__ Wrapper.dll  
                         |_______Loads___1.dll  
                                           |___ Depends___ xyz.dll  

Following is the file Structure

Root
  |___A  
  |   |__Process.exe  
  |___B  
      |__Wrapper.dll   
      |__C
         |__1.dll
         |__xyz.dll

I am using Assembly.LoadFile(@"c:\root\B\C\1.dll"); function to load the 1.dll in Wrapper.dll however it fails to find the dependencies as it tries to find the dependencies in A dir, the same directory where we have process executable.

like image 781
Mubashar Avatar asked Jan 07 '23 10:01

Mubashar


1 Answers

I found the solution I just changed the Assembly.LoadFile to Assembly.LoadFrom and its loaded perfectly fine. You can read the explanation here However following is the crust of it.

Load-From Context

The load-from context lets you load an assembly from a path that is not under the application path, and therefore is not included in probing. It enables dependencies to be located and loaded from that path, because the path information is maintained by the context. In addition, assemblies in this context can use dependencies that are loaded into the default load context.

like image 59
Mubashar Avatar answered Jan 08 '23 23:01

Mubashar