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=, Culture=neutral, PublicKeyToken=null'".

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Your classs TestAssembly needs to inherit from MarshalByRefObject for this to work.  Cross appdomain calls are marshalled by the .NET Remoting
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.

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)



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.