Nipun
Nipun

Reputation: 1485

AppDomain.CreateInstance

When we used AppDomain.CreateInstance("Assembly name", Type name) and my class inherits from MarshalByRefObject what happen internally? Is it create a TransparetnProxy?

Code:

class Greet : MarshalByRefObejct
{
  ...
}

class test
{
 public static void Main(string[] args)
 {
   AppDomain ad = AppDomain.CreateDomain("Second");
   ObjectHandle hObj = ad.CreateInstance("Test", args[0]);
  ....
 }
}

passing in args[0] = Greet

Upvotes: 1

Views: 4406

Answers (3)

dilip sharma
dilip sharma

Reputation: 11

AppDomain.CoCreateInstance() takes two argument that which assembly and their type name.

And it returns an ObjectHandle used to create the instance of specified type in the assembly, and also provide the handle by which wrap(serialized) when it is instantiated and when it need then it is unwrap(deserialized) return to real Proxy.

Upvotes: 1

Szymon Rozga
Szymon Rozga

Reputation: 18178

Yes.

You might also want to take a look at CreateInstanceAndUnwrap. If your code in Main and the Greet class were to share a common interface, you could cast hObj into your interface and call methods on it using the TransparentProxy.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503030

Yes, it creates a transparent proxy, which you get by unwrapping the object handle.

I find the documentation and example for ObjectHandle.Unwrap is quite informative, as is the general MarshalByRefObject documentation.

Upvotes: 3

Related Questions