Activator.CreateInstanceFrom and referenced assemblies-Collection of common programming errors
After reading “Discover Techniques for Safely Hosting Untrusted Add-Ins with the .NET Framework 2.0” by Shawn Farkas in November 2005 MSDN issue (
) I wrote a simple program to test it. The directory structure is (directories are in bold):
HostApp HostApp.exe Interfaces.dll AddinMgr.dll
Addins
Test
TestAddin.dll
Interfaces.dll assembly has two interfaces: IHost and IAddin. AddinMgr.dll contains one class AddinManager which implements IHost. TestAddin.dll contains one class Test which implements IAddin.
I create a new application domain with ApplicationBase pointing to Addins\Test and then use Activator.CreateInstanceFrom to create an instance of AddinManager class in a new domain. Then AddinManager calls Activator.CreateInstance to create an instance of Test class. This is where the problem is: Activator.CreateInstance throws FileNotFoundException (could not load file or assembly ‘Interfaces’). But AppDomain.GetAssemblies() shows that Interfaces.dll was loaded in the new domain. If I copy Interfaces.dll in the Addins\Test directory then everything works.
So, my question is: how does CreateInstanceFrom(AppDomain appDomain, string assembly, string type) really works? It looks like assemblies loaded with CreateInstanceFrom are being treated differently by the runtime.
Alex