views:

1385

answers:

3

I am confused about the serialization sample from MSDN,

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.getobjectdata.aspx

My confusion is, in method GetObjectData (which is called during serialization), will the method,

(1) serialize both the additional data (in method GetObjectData from AddValue) and the fields/properties of the class; (2) or just write the data in method GetObjectData without writing fields/properties of the class?

I have debugged seems (2) is correct -- no fields/properties data are serialized if GetObjectData method is used? Is that correct? (I am not an expert and just want to confirm here, but 100% confident about myself.)

+1  A: 

If you implement ISerializable, you are reasponsible for all data (i.e. scenario "2" in your question); nothing extra is serialized automatically. What is your requirement? Things like DataContractSerializer can be property-based, allowing you to decorate both the regular fields and your custom property (that has some logic) and have them serialized properly. If you need binary (for space etc), then perhaps consider things like protobuf-net, which mixes the two while being space efficient.

So: what are your requirements?

Data Contract example:

[DataContract]
public class Foo {
    [DataMember]
    public int Bar {get;set;} // simple data

    [DataMember]
    private string DoSomeThinking {
        get {.... serialize the complex data ....}
        set {.... deserialize the complex data ....}
    }
}
Marc Gravell
Or for binary with the above, you can use NetDataContractSerializer: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.netdatacontractserializer.aspx
Marc Gravell
Thanks Marc!what is the differences between implementing ISerializable interface v.s. mark with [Serializable] attribute?
George2
Marking it as [Serializable] simply says "it is OK to serialize this"; most serialization engines will detect this and automatically serialize fields (implementation) or properties (contracts). ISerializable is used by BinaryFormatter for **custom** serialization, where *you* control the binary.
Marc Gravell
+2  A: 

If you implement ISerializable you must add all data (at least the data needed to deserialize) including all fields to the SerializationInfo using AddValue.

Jakob Christensen
Is it the same to implement ISerializable interface v.s. mark with [Serializable] attribute?
George2
If you implement ISerializable you must also mark your class with [Serializable]. If you do not wish to add any extra information to the serialization and your class is fairly simple you do not have to implement ISerializable.
Jakob Christensen
All you have to do is mark with [Serializable] and the runtime will do all the work.
Jakob Christensen
Still confused. What is the additional function we could achieve by implementing ISerializable?
George2
If you need to control the (de-)serialization you can do this through ISerializable. You only need to do this in rare cases. For example you can use it to control versioning if your class changes over time.
Jakob Christensen
Thanks Jakob, I have tested that if marked with Serializable attribute, all public fields will be serialized automatically, but if both marked with Serializable and implements ISerializable interface (but makes GetObjectDate empty), nothing will be written. So strange. Any comments?
George2
If you implement ISerializable you have to do everything yourself. Nothing happens automatically. BTW, not only public fields will be serialized. Private and protected fields will also be included automatically when using SerializableAttribute.
Jakob Christensen
Thank you for your comments, Jakob!
George2
+2  A: 

Im not sure what you want to achieve but isn't easier to let C# do the work for you:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Test
{
    [Serializable]
    public class TestObject
    {
        private String name;
        private String note;
        #region Getters/setters

        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        public String Note
        {
            get { return note; }
            set { note = value; }
        }
        #endregion
    }
}

Now you can use the XmlSerializer or BinaryFormatter to (de)serialize the object

PoweRoy
Is it the same to implement ISerializable interface v.s. mark with [Serializable] attribute?
George2
From MSDN:"Any class that might be serialized must be marked with the SerializableAttribute. If a class needs to control its serialization process, it can implement the ISerializable interface."The result is the same but the attribute does the work for you
PoweRoy
Thanks PoweRoy, I have tested that if marked with Serializable attribute, all public fields will be serialized automatically, but if both marked with Serializable and implements ISerializable interface (but makes GetObjectDate empty), nothing will be written. So strange. Any comments?
George2
I haven't used the interface before so I can't comment. Do you need the interface explicit? if not, just stick with the attribute.This helped me alot http://www.ondotnet.com/pub/a/dotnet/2002/08/26/serialization.html?page=1
PoweRoy
Good link, thanks PoweRoy!
George2