Link to home
Start Free TrialLog in
Avatar of gromul
gromul

asked on

Load assembly from a file into app domain

How do I load assembly from a file into app domain? I need to load into a non-current app domain so I could unload the domain (and the loaded assembly in it) and then load another version of the assembly.

Here's what I have so far:

AppDomainSetup appDomainSetup = new AppDomainSetup( );
AppDomainSetup appDomainSetup.ShadowCopyFiles = "true";
AppDomainSetup appDomainSetup.ApplicationBase = @"c:\Documents and Settings\user\Local Settings\Temp";

AppDomain appDomain = AppDomain.CreateDomain( "Test", null, appDomainSetup );
string filePath = @"C:\Documents and Settings\user\Local Settings\Temp\Test.dll";
string typeName = "Test.TestAssembly";
object o = appDomain.CreateInstanceFromAndUnwrap( filePath, typeName );

The latest error I'm getting is "Type is not resolved for member 'Test.TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'".

Thanks
Avatar of gbzhhu
gbzhhu
Flag of United Kingdom of Great Britain and Northern Ireland image

Your classs TestAssembly needs to inherit from MarshalByRefObject for this to work.  Cross appdomain calls are marshalled by the .NET Remoting
Avatar of gromul
gromul

ASKER

What if the TestAssembly class already inherits from another class over which I don't have control? Can interfaces be used as an alternative? I tried implementing ISerializable but that didn't work.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of gbzhhu
gbzhhu
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial