• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2446
  • Last Modified:

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
0
gromul
Asked:
gromul
  • 2
1 Solution
 
gbzhhuCommented:
Your classs TestAssembly needs to inherit from MarshalByRefObject for this to work.  Cross appdomain calls are marshalled by the .NET Remoting
0
 
gromulAuthor Commented:
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
0
 
gbzhhuCommented:
I am not sure if an interface will work.  What about using inner class.  Wrap another class around TestAssembly so that it becomes an inner class and have the wrapper class inherit from MarshalByRefObject

Simple inner class example (I am sure there are better ones online)

http://www.csharphelp.com/archives/archive113.html

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now