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

发布于 2024-08-04 04:21:26 字数 348 浏览 3 评论 0原文

在 AppDomain A 中,我有一个 T 类型的对象 oT 既不是Serialized,也不是从 MarshalByRefObject 派生的。类型 T 由插件主机提供,我无法控制。

我想创建一个 AppDomain B 并将代理传递给 oB 中的方法,但我很困惑:如何创建代理?

B 中的方法应该能够调用 o 上的方法并读取属性等。这些方法的结果必须以类似的方式进行代理。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鱼窥荷 2024-08-11 04:21:26

我建议您创建一个适当的代理对象,该对象实现与您尝试代理的对象相同的接口,并且继承自 MarshalByRefObject。然后,您远程代理对象。在服务器端,代理将委托给您的对象。

根据您的要求,服务器对象将包含您的对象作为静态(所有客户端看到相同的对象)或非静态(每个客户端获取一个新副本)。

对于静态成员,您需要在服务器中创建代理并使用您的对象对其进行初始化,或者第一个分配的代理(当第一个客户端连接时)创建您的对象并初始化自身。我用过前者。

当然不要忘记租赁。

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.

夏末 2024-08-11 04:21:26

如果您想要一个代理,最好的选择可能是将对象封装在从 MarshalByRefObject 继承的类型中(作为私有字段) ),并且具有公共方法等使其可用;本质上是一个门面。

如果您想要序列化 ​​- 我会使用与对象相关的 DTO,但采用不同的(可序列化)类型。只需发送状态,并在另一端重建实际类型。

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.

自我难过 2024-08-11 04:21:26

你不能。 AppDomain 之间通信的唯一方法是使用代理或使用副本(即可序列化)。

您可以将您的类型包装在继承自 MarshalByRefObject 的代理中并使用它吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文