Difference between Serializable and MarshalByRefObject

后端 未结 2 1450
春和景丽
春和景丽 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:09

    Adding to @edokan answer, Serializable attribute basically marks a type as being meant to be serialized (i.e, converted to a byte stream).

    Frameworks (such as ORM mappers, oodbs, persistence engines) may use this information to take decisions such as whenever the state of objects should be persisted in databases, sent over a network, etc.

    You can use classes such BinaryFormatter to take an object marked with the Serializable attribute and create byte stream from it and store it in a file and/or send it over network and latter, reconstruct your object again from these streams.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题