Difference Between CurrentDomain.CreateInstance and Activator.CreateInstance

B

b0yce

Hi all,

I have been looking around the newsgroups and various resources but have
found no clear distinct answer to when and why you should use
AppDomain.CreateInstance or Activator.CreateInstance when loading Assemblies
dynamically.

My scenario is;

HostA is a service, when it starts it creates an instance of
HostedObjectA and puts this on a TCP port ready for remote communication as
a singleton. This is all through a reference.

ClientA connects to HostedObjectA via the TCP channel remotely, then via
the proxy, ClientA calls a method on HostedObjectA passing FileName &
TypeName of an assembly to load and instantiate within HostedObjectA.
HostedObjectA then loads up the assembly using either
Activator.CreateInstance(...).UnWrap() or
AppDomain.CreateInstance(...).UnWrap() methods.

The Activator method works fine and creates the object, but the
AppDomain method does not work and throws "File or assembly name [filename],
or one of its dependencies, was not found.".

I then created a dummy test, with all files not having any dependencies,
apart from the normal system references, but still get the same outcome.

I am fine with that Activator works, but I would like to know why it
does and why AppDomain.CreateInstance does not work, it's makes for better
undesrtanding. :)

So anyone care to enlighten?

Cheers in advance
b0yce
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top