c# - Class library loses references (dlls) when being used -
sorry title. don't know how describe problem shortly. problem have class-library has references other (third party) dlls. need use class-library in project, added .dll of class-library main-project.
when start main-project, there's alway error says, reference (dll) in class-library cannot found.
if add whole class-library project projectmap in visual studio , reference whole project, error doesn't occur.
i don't want add whole class-library project every "host"-project make.
has idea why error occurs when .dll of class-library added, not when whole project of class-library added reference?
there must solution working if don't add whole library-project reference. otherwise wouldn't make sense make class library, right?
by way: class-library contains third-party dlls , local copy property of third-party dll set true.
thanks in advance!
edit: goal make class-library portable, though contains third-party libraries. want give .dll pc , use without adding whole class-library project every time.
the error because you're not copying dll's on second project, added reference dll get's copied, not dll's referenced dll, there missing libraries.
or redistribute dependencys dll or can embedd dll's inside dll resources , intercept assembly load , provide through resource: http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx
edit: in order inside dll need use static class , call static initializer before using of classes dependant on other libraries.
here example setup:
-a library called libraryb supplies simple class this:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace libraryb { public class referencedclass { public int getit() { return 5; } } } -a library called librarya references libraryb , supplies 2 classes, initializer , real class:
initializer
using system; using system.collections.generic; using system.linq; using system.reflection; using system.text; using system.threading.tasks; namespace librarya { public static class initializer { public static void init() { appdomain.currentdomain.assemblyresolve += (sender, args) => { if (!args.name.startswith("libraryb")) return null; return assembly.load(librarya.properties.resources.libraryb); }; } } } class
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace librarya { public class realclass { public int doit() { libraryb.referencedclass cl = new libraryb.referencedclass(); return cl.getit(); } } } the librarya has libraryb.dll compiled library embedded resource.
-a project called test references librarya:
using librarya; using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace consoleapplication1 { class program { static void main(string[] args) { initializer.init(); realclass c = new realclass(); console.writeline("from librarya: " + c.doit()); console.readkey(); } } } if set-up everithing right , execute it work, remember if doing through visual studio, vs copy dll's real test after compiling copy exe , librarya , execute, work without libraryb , libraryb being used librarya.
Comments
Post a Comment