MarshalByRefObject 很特别吗?

发布于 2024-08-30 09:20:50 字数 632 浏览 6 评论 0原文

.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 技术交流群。

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

发布评论

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

评论(2

花开柳相依 2024-09-06 09:20:50

神奇之处似乎在于一个特殊的 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.

秋叶绚丽 2024-09-06 09:20:50

我相信 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.

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