tags:

views:

124

answers:

3

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.

+2  A: 

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?

Brian Rasmussen
+3  A: 

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.

Marc Gravell
Yes, I have been contemplating a facade. Are there any libraries for creating facades for a *whole* API? I'm looking into code generation right now...
Daren Thomas
None that I know of
Marc Gravell
+4  A: 

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.

Richard