Ali Sepehri.Kh
Ali Sepehri.Kh

Reputation: 2508

Create instance of a type in another domain

I'm reading a code which is creating an instance of a type in another Domain by reflection. Why do we need to do this? What is the advantages of this kind of instance creation?

AppDomain _domain = AppDomain.CreateDomain("ServerImporterDomain");
var type = typeof (ServerImporter);
ServerImporter si = _domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName) as ServerImporter;

Upvotes: 0

Views: 713

Answers (1)

Ryan M
Ryan M

Reputation: 2112

Some reasons for this would be:

  • Security sandboxing. The app domain that you create can run at a lower trust level than the main application. This doesn't seem to be the case here as you are not passing an AppDomainSetup to the CreateDomain call.
  • Memory management. If a given assembly requires large amounts of memory and you know that you only need to use that assembly for a short time, you can load it into its own domain and then unload it when you're done with it.
  • Different domains can handle uncaught exceptions differently and have different ProcessExit handlers.

A good writeup is at http://blogs.msdn.com/b/cclayton/archive/2013/05/21/understanding-application-domains.aspx.

Upvotes: 1

Related Questions