如何创建不是从 MarshalByRefObject 派生的远程对象的代理?
在 AppDomain A
中,我有一个 T
类型的对象 o
。 T
既不是Serialized
,也不是从 MarshalByRefObject
派生的。类型 T
由插件主机提供,我无法控制。
我想创建一个 AppDomain B
并将代理传递给 o
到 B
中的方法,但我很困惑:如何创建代理?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您创建一个适当的代理对象,该对象实现与您尝试代理的对象相同的接口,并且继承自 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.
如果您想要一个代理,最好的选择可能是将对象封装在从
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.
你不能。 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?