c# - .NET add directory for dependencies at runtime -


from main c# application instantiate application via reflection.

assembly.createinstance( ... ) 

however other assembly relies on dlls in directory executing assembly. how can add directory lookup path?

here how implement need in ndepend.powertools. these set of tools based on ndepend.api. dll ndepend.api.dll in directory .\lib while ndepend.powertools.exe assembly inn directory .\.

the ndepend.powertools.exe main() method looks like:

[stathread] static void main() {        appdomain.currentdomain.assemblyresolve += assemblyresolverhelper.assemblyresolvehandler;    mainsub(); }  // mainsub() here avoids main() method uses // ndepend.api without having registered assemblyresolvehandler [methodimpl(methodimploptions.noinlining)] static void mainsub() {    ... 

and assemblyresolverhelper class is:

using system; using system.diagnostics; using system.reflection;   namespace ndepend.powertools {    internal static class assemblyresolverhelper {       internal static assembly assemblyresolvehandler(object sender, resolveeventargs args) {          var assemblyname = new assemblyname(args.name);          debug.assert(assemblyname != null);          var assemblynamestring = assemblyname.name;          debug.assert(assemblynamestring != null);           // special treatment ndepend.api , ndepend.core because defined in $ndependinstalldir$\lib          if (assemblynamestring != "ndepend.api" &&              assemblynamestring != "ndepend.core") {             return null;          }          string binpath =               system.io.path.getdirectoryname(assembly.getexecutingassembly().location) +               system.io.path.directoryseparatorchar +               "lib" +               system.io.path.directoryseparatorchar;           const string extension = ".dll";           var assembly = assembly.loadfrom(binpath + assemblynamestring + extension);          return assembly;       }    } } 

this works within single appdomain. wouldn't use appdomain here if not needed. appdomain pretty costly facility (in terms of performance) + thread jumps appdomains boundaries, has serialize/unserialize in/out data feed assembly code, , can headache.

the advantage of appdomain lets unload loaded assemblies. if expect load/unload assemblies on regular basis within life of main appdomain, using temporary appdomain way go.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -