I am trying to load a DLL file that has been copied into a byte array into a new AppDomain.
The DLL does contain references to things like Windows.Forms and other dlls. Are those the ones failing to load? If so how do you pre-load them for that specific domain?
AppDomainSetup Setup = new AppDomainSetup();
Setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
Setup.ApplicationName = "Plugin_" + DLLName + "" + PluginManager.PluginList.Count;
AppDomain Domain = AppDomain.CreateDomain("Domain_" + DLLName + "" + PluginManager.PluginList.Count, null, Setup);
Assembly Assembly = Domain.Load(buffer);
However changing
Assembly Assembly = Domain.Load(buffer);
to
Assembly = AppDomain.CurrentDomain.Load(buffer);
Makes it work.
I need it to be in a separate domain because I plan on unloading this AppDomain to unload the DLL itself.
I tried playing around with the "AssemblyResolve" event like everyone out there is suggesting but it doesn't do anything.
Also the reason I need this to be from a byte array is because I want to be able to switch the DLL file at run-time and re-load it into memory.
The DLL files are in a separate folder from the .exe file. It's in the same directory just one folder inside.
Interesting discovery:
If I add the DLL files to the file location of the .exe it will load those and lock onto them and load successfully into the new domain. Why does it work this way when I feed it a byte array and not the file location? Do I actually have to take the byte array and write to a temporary file? I can do that and delete them when I'm done with them but it seems like a waste of time, there's no reason it can't do it all from memory.