Loading .dll/.exe from file into temporary AppDomain throws Exception
Posted
on Stack Overflow
See other posts from Stack Overflow
Published on 2009-09-23T15:52:26Z
Indexed on
2010/05/11
16:34 UTC
Read the original article
Hit count: 256
.NET
|assemblies
Hi Gang, I am trying to make a Visual Studio AddIn that removes unused references from the projects in the current solution (I know this can be done, Resharper does it, but my client doesn't want to pay for 300 licences). Anyhoo, I use DTE to loop through the projects, compile their assemblies, then reflect over those assemblies to get their referenced assemblies and cross-examine the .csproj file.
Problem: since the .dll/.exe I loaded up with Reflection doesn't unload until the app domian unloads, it is now locked and the projects can't be built again because VS tries to re-create the files (all standard stuff). I have tried creating temporary files, then reflecting over them...no worky, still have locked original files (I totally don’t understand that BTW). Now I am now going down the path of creating a temporary AppDomain to load the files into and then destroy. I am having problems loading the files though:
The way I understand AddDomain.Load is that I should create and send a byte array of the assembly to it. I do that:
FileStream fs = new FileStream(assemblyFile, FileMode.Open);
byte[] assemblyFileBuffer = new byte[(int)fs.Length];
fs.Read(assemblyFileBuffer, 0, assemblyFileBuffer.Length);
fs.Close();
AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ApplicationBase = assemblyFileInfo.Directory.FullName;
AppDomain tempAppDomain = AppDomain.CreateDomain("TempAppDomain", null, domainSetup);
Assembly projectAssembly = tempAppDomain.Load(assemblyFileBuffer);
The last line throws an exception:
"Could not load file or assembly 'WindowsFormsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.":"WindowsFormsApplication3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"}"
Any help or thoughts would be greatly appreciated. My head is lopsided from beating it against the wall...
Thanks, Dan
© Stack Overflow or respective owner