c# - Is it possible to switch DLLs at runtime so as to use a different version? -
i have application contains number of plugins (mef) connect number of different i/o devices. of these plugins have number of managed , unmanaged dlls.
one manufacturer has released new firmware , new drivers. api remains same.
i thought include both dll versions in separate resource folders , copy required set output folder when application starts up.
(i guess make second copy of plugin , figure out way of loading correct 1 thought copying dlls might easier - considering plugin code unchanged)
this not work.
static myclass() // static constructor { // required version? if (envvar.equals("4") ) { path = "ver4.1.1"; } else { path = "ver2.5.1"; } // location of drivers path = path.combine("./prj/qx", path); string[] files = directory.getfiles(path); // copy files , overwrite destination files if exist. foreach (string s in files) { string filename = path.getfilename(s); string destfile = path.combine(".", filename); file.copy(s, destfile, true); } // force load of assemby assembly assy = loadwithoutcache("driver.dll"); string fn = assy.fullname; } static assembly loadwithoutcache(string path) { using (var fs = new filestream(path, filemode.open)) { var rawassembly = new byte[fs.length]; fs.read(rawassembly, 0, rawassembly.length); return assembly.load(rawassembly); } }
the error message suggests original dll loaded or should have been loaded not be.
could not load file or assembly 'driver, version=1.5.77, culture=neutral, publickeytoken=983247934' or 1 of dependencies. located assembly's manifest definition not match assembly reference. (exception hresult: 0x80131040)
is there way achieve goal or have build 2 separate versions of application?
edit should not necessary have both dlls loaded @ same time. enough start application parameter causes 1 or other dll loaded.
you in several ways. 1 possibility use managed extensibility framework (mef). monitor folder dlls reside , load new version new 1 becomes available.
the other possibility can use reflection dynamically load dll want during runtime.
Comments
Post a Comment