问题
I'm wondering if a setup like this is possible:
c:\eflow\proxy.dll (main DLL loaded by application) c:\eflow\application\dynamic.dll (DLL dynamically loaded by proxy.dll) c:\eflow\dependency.dll (dependent DLL required by dynamic.dll)
Basically, I'd like to dynamically load a DLL (to instantiate classes, etc) but have that DLL's dependencies stored in a different location.
Is this possible? I don't want to have a copy of these dependent DLLs in every sub-directory... (and I can't load them in the GAC because they aren't signed and they are 3rd party DLLs)
回答1:
Yes you can do this.
By handling the AppDomain.AssemblyResolve Event you can load dependant assemblies from wherever you like. If your application knows that dependant assemblies may be located in another directory then it can handle this event (which automatically implies that they were not found in the applciation directory as the .Net framework looks there first), and attempt to load the assembly from that alternate location.
See Resolving Assembly Loads for more details.
Update: In this case I beleive that you can instead add the given directory to the AppDomainSetup.PrivateBinPath Property for the desired app domain. This will only work if the given directories are subdirectories of the application base directory. If this is not the case then to use this approach you would need to create a new AppDomain with a suitable application base directory.
Also see Best Practices for Assembly Loading
来源:https://stackoverflow.com/questions/6815529/can-i-specify-dependency-directories-when-dynamically-loading-assemblies