Difference between Serializable and MarshalByRefObject

后端 未结 2 1446
春和景丽
春和景丽 2021-02-06 15:28

I am very confused with [Serializable] and :MarshalByRefObject attribute in .NET Remoting .However i read many article on that

2条回答
  •  太阳男子
    2021-02-06 16:11

    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
    

    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.

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

提交回复
热议问题