MarshalByRefObject 很特别吗?
.NET 有一个称为远程处理的东西,您可以在不同的应用程序域甚至物理机之间传递对象。我不完全理解魔法是如何完成的,因此有这个问题。
在远程处理中,有两种传递对象的基本方法 - 要么可以将它们序列化(转换为一堆字节并在另一端重建),要么可以从 MarshalByRefObject,在这种情况下,.NET 会创建一些透明代理,并且所有方法调用都会转发回原始实例。
这非常酷,而且就像魔术一样。而且我不喜欢编程中的魔法。使用 Reflector 查看 MarshalByRefObject
时,我没有看到任何可以将其与任何其他典型对象区分开的东西。甚至没有一个奇怪的内部属性或任何东西。那么整个透明代理是如何组织的呢?我自己能做一个这样的机制吗?我可以制作一个替代的 MyMarshalByRefObject
吗?它不会从 MarshalByRefObject
继承,但仍具有相同的行为?或者 MarshalByRefObject
是否受到 .NET 引擎本身的特殊处理,并且整个远程处理壮举是普通人无法复制的?
.NET has a thing called remoting where you can pass objects around between separate appdomains or even physical machines. I don't fully understand how the magic is done, hence this question.
In remoting there are two base ways of passing objects around - either they can be serialized (converted to a bunch of bytes and the rebuilt at the other end) or they can inherit from MarshalByRefObject, in which case .NET makes some transparent proxies and all method calls are forwarded back to the original instance.
This is pretty cool and works like magic. And I don't like magic in programming. Looking at the MarshalByRefObject
with the Reflector I don't see anything that would set it apart from any other typical object. Not even a weird internal attribute or anything. So how is the whole transparent proxy thing organized? Can I make such a mechanism myself? Can I make an alternate MyMarshalByRefObject
which would not inherit from MarshalByRefObject
but would still act the same? Or is MarshalByRefObject
receiving some special treatment by the .NET engine itself and the whole remoting feat is non-duplicatable by mere mortals?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
神奇之处似乎在于一个特殊的
TransparentProxy
类 - .NET 运行时以特殊方式处理它。我认为 MarshalByRefObject 可能包含一些额外的内部信息,这对这种机制很有帮助,但我没有对此进行太多研究。
The magic seems to be in a special
TransparentProxy
class - the .NET Runtime handles it in a special way.I think that
MarshalByRefObject
may contain some additional internal information which can be helpful for this mechanism, but I haven't looked much into that.我相信 MarshalByRefObject 并不是那么特别。我相信它存在的全部原因在于它的生命周期管理以及它在服务器上的垃圾收集方式。 LifetimeServices 类文档。
AFAIK,远程处理的真正魔力是在您设置主机时由远程基础设施自己完成的。 MarshalByRefObject 并未执行跨 AppDomain 编组内容的任何实际工作。
I believe MarshalByRefObject isn't all that special. I believe that its whole reason for existence lies with its lifetime management and how it's garbage-collected on the server. There are some good comments on what this is about in the LifetimeServices class documentation.
AFAIK, the real magic of remoting is done by the remoting infrastructure yourself when you set up the hosts. MarshalByRefObject isn't doing any of the real work of marshalling stuff across AppDomains.