Daren Thomas
Daren Thomas

Reputation: 70324

How can I create a proxy to a remote object not derived from MarshalByRefObject?

In AppDomain A I have an object o of type T. T is neither Serializable nor derived from MarshalByRefObject. Type T is provided by a plugin host over which I have no control.

I would like to create an AppDomain B and pass a proxy to o to a method in B, but am stumped: How to create the proxy?

The method in B should be able to invoke methods on o and read properties etc. The results of these methods will have to be proxied in a similar fashion.

Upvotes: 2

Views: 1807

Answers (3)

Richard
Richard

Reputation: 192

What I suggest is that you create a proper proxy object that implements the same interface as the object you're trying to proxy and also inherits from MarshalByRefObject. You then remote the proxy object. On the server side the proxy would delegate to your object.

Depending on your requirements the server object would contain your object as a static (all clients see the same object) or as non static (each client gets a new copy).

In the case of a static member, either you need to create the proxy in the server and initialise it with your object, or the first allocated proxy (when the first client connects) creates your object and initialises itself. I've used the former.

Of course don't forget about leases.

Upvotes: 5

Marc Gravell
Marc Gravell

Reputation: 1062895

If you want a proxy, your best bet would probably be to encapsulate the object inside a type that does inherit from MarshalByRefObject (as a private field), and which has public methods etc to make it usable; a facade, essentially.

If you want serialization - I'd use a DTO that is related to the object, but in a distinct (serializable) type. Just send the state, and reconstruct the actual type at the other end.

Upvotes: 3

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

You can't. The only way to communicate between AppDomains is by using a proxy or by using a copy (i.e. serializable).

Could you wrap your type in a proxy that inherits from MarshalByRefObject and use that instead?

Upvotes: 2

Related Questions