views:

767

answers:

1

Is it better to use System.Assembly.Load or to load the assembly directly into the AppDomain using System.AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap? I am specifically interested in calling different versions of the same assembly running in the same process. I think with CreateInstanceFromAndUnwrap your assembly must have the [serializable] attribute, but why is that?

EDIT: The assembly I'm trying to load is NOT in the same location as the caller.

A: 

By not the same location, I am guessing you mean elsewhere on the filesystem, let me know if not.

It's better to use System.Assembly.Load, as it is the generic low-level assembly loader.

Also, you mention wanting to load different versions. For this you'll have to set up secondary AppDomain to load the assembly into, then unload that AppDomain when you're done and create a new one for the new assembly version, as .Net doesn't support unloading an assembly from a given AppDomain.

The CreateInstanceFromAndUnwrap is for loading code that has been "sent" to you somehow, hence the things that it works upon must be serializable.

Macker
If your assemblies keep the version number in the name, you wouldn't *have* to unload the previous one... although depending on the number of assemblies you load, your RAM usage would increase proportionately...
I thought you *could* load two versions of the same assembly in the same appdomain... Not saying I have ever had the need to try it.
JasonRShaver
According to [this post](http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/ff1a1d47-c3b8-422f-a373-6fe4b90e36d8), "...you should be able to load 2 different versions of the same assembly into same AppDomain, **if the assembly has strong name** (name, version, culture and public key)." Not exactly rigid documentation though.
bacar