views:

1697

answers:

1

What is the proper way to load a .NET assembly into a seperate AppDomain so you can have access to its Types/Classes but still be able to unload it (and reload it).

This is a tangent of this previous discussion: http://stackoverflow.com/questions/1137781/c-correct-way-to-load-assembly-find-class-and-call-run-method

+1  A: 

Basically, you just create the new AppDomain, and then call AppDomain.CreateInstanceAndUnwrap to create the type. Here's a simple CodeProject article about this process.

There are some tricks, here. You can't ever refer to the Type directly (this will load the type into your current AppDomain), and the objects should all derive from MarshallByRefObj. This will allow you to use the object "remotely", meaning keep it from loading into your AppDomain.

Reed Copsey
+1 thanks. How bad is the performance penalty each time you cross AppDomains?
tyndall
It depends. Anything passing between teh appdomains is basically serialized across. (It works very similarly to Remoting.) In general, if all of your types derive from MarshallByRefObj, they will only serialize a "handle" across, so it's quite fast. When you read the data, the data will be serialized, though, which can slow things down a bit. Try to always keep as much "internal" in the second appdomain, and it probably wont' be an issue.
Reed Copsey