简体   繁体   English

如何为不是从MarshalByRefObject派生的远程对象创建代理?

[英]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 . 在AppDomain A我有一个类型为T的对象o T is neither Serializable nor derived from MarshalByRefObject . T既不是Serializable也不是从MarshalByRefObject派生的。 Type T is provided by a plugin host over which I have no control. 类型T由插件主机提供,我无法控制。

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? 我想创建一个AppDomain B并将代理传递给oB的方法,但是我很难过:如何创建代理?

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. B的方法应该能够调用o方法和读取属性等。这些方法的结果必须以类似的方式代理。

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. 我建议您创建一个正确的代理对象,该对象实现与您尝试代理的对象相同的接口,并且还继承自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. 当然不要忘记租约。

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; 如果你想有一个代理 ,你最好的选择很可能是封装, 继承一个类里面的对象MarshalByRefObject (作为一个私有域),以及具有公共方法等,使其可用; a facade, essentially. 一个外观,基本上。

If you want serialization - I'd use a DTO that is related to the object, but in a distinct (serializable) type. 如果你想要序列化 ​​- 我使用对象相关的DTO,但是使用不同的(可序列化)类型。 Just send the state, and reconstruct the actual type at the other end. 只需发送状态,并在另一端重建实际类型。

You can't. 你不能。 The only way to communicate between AppDomains is by using a proxy or by using a copy (ie serializable). 在AppDomains之间进行通信的唯一方法是使用代理或使用副本(即可序列化)。

Could you wrap your type in a proxy that inherits from MarshalByRefObject and use that instead? 你可以将你的类型包装在继承自MarshalByRefObject的代理中并使用它吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM