So my problem revolves around saving memory.
In essence I need to load an assembly in to a separate app domain other than the main/current domain, check for types within that assembly, and then unload the new domain when done.
Currently my solution is as follows:
AppDomain NewDomain = AppDomain.CreateDomain("newdomain");
foreach(string path in dllPaths) //string list of dll paths
{
byte[] dllBytes = File.ReadAllBytes(dll);
NewDomain.Load(dllBytes); //offending line
}
DoStuffWithNewDomain();
AppDomain.Unload(NewDomain);
The NewDomain.Load line seems to load the assembly into the new domain but also into the current domain of my program.
I used this link as a reference - http://www.csharp411.com/how-to-load-a-net-assembly-into-a-separate-appdomain-so-you-can-unload-it/
Many thanks :)
As Erik suggested, you need to use CreateInstanceAndWrap. Here's an example I did in VB a while back...
As mentioned already, loading the assembly from bytes can only happen for the current app domain.
Here is one way to context-switch to make your target app domain the current one, using the
DoCallBack
method on the app domain (http://msdn.microsoft.com/en-us/library/system.appdomain.docallback%28v=vs.110%29.aspx).After the assemblies are loaded you can then use the same method to inspect the assemblies and types of the new app domain, and create instances as necessary, before unloading it.