Serializable和MarshalByRefObject之间的区别 [英] Difference between Serializable and MarshalByRefObject

查看:83
本文介绍了Serializable和MarshalByRefObject之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 .NET Remoting 中的 [可序列化] :MarshalByRefObject 属性感到非常困惑,但是我读了很多有关此的文章。如果我使用[Serializable]属性,则所有方法都完全在调用者域中执行,这意味着不同的调用者应在自己的域中拥有不同方法的副本,并且一个调用者所做的任何更改都不应反映在其他调用者上。我在程序中实现的事情,没有得到期望的结果。

I am very confused with [Serializable] and :MarshalByRefObject attribute in .NET Remoting .However i read many article on that. If i am using [Serializable] attribute then all methods are executed entirely in callers domain, it means that different callers should have copy of different methods on their own domain and any alteration done by one caller should not reflect on others.But when the same thing i am implementing in my program, i am not getting the desired result.

推荐答案

可序列化属性用于对一个对象进行签名,当通过远程处理请求该对象时,该对象将被序列化为一个字节数组,作为字节数组从源传输到目标,然后从该字节数组重建另一个对象实例,即在目标环境(AppDomain或Application)中生活和呼吸。在该对象上执行的操作不会反映到源。要更新对象,您必须再次访问源并发送更改后的对象(当然,作为新的字节数组)。就像下载文件并对其进行修改一样,所做的更改不会反映到您从中下载文件的服务器。

Serializable attribute is used to sign an object that when it is requested through remoting, it will be serialized to a byte array, transferred as byte array from source to destination and then another instance of object is rebuild from that byte array that lives and breathes in destination environment (AppDomain or Application). Manipulations performed on that object is not reflected to the source. To update object you have to make another trip to the source and send your changed object (as a new byte array of course). it is just like downloading a file and modifying it, your changes are not reflected to the server where you have downloaded file from.

var user = server.GetUser("edokan");
user.Alias = "edokan2";

var user2 = server.GetUser("edokan");
//user.Alias == user2.Alias; // is false

另一方面 MarshalByRefObject 标记您的对象,而不是对象的数据,而是通过远程处理对对象的引用,并且在服务器端执行每个方法调用/每个属性操作。这就像将您的问题发布到StackOverflow并阅读答案一样,您只有浏览器和用于查看/处理问题的URL。一切都在StackOverflow服务器上执行。

On the other hand MarshalByRefObject marks your object that instead of data of your object, a reference to your object is traveled through remoting and every method call/every property manipulation is performed on server side. This is just like posting your question to StackOverflow and reading answers, you have nothing but a browser and a url to view/manipulate question. Everything is performed on StackOverflow servers.

您的困惑起于非常简单的点,MS使远程处理变得如此简单,实际上,人们认为一切都在客户端。

Your confusion arises from a very simple point, MS made remoting sooo simple, one actually thinks that everything is on client side.

这篇关于Serializable和MarshalByRefObject之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆