Call GetObjectData to Serialize

I am confused in the serialization example from MSDN .

My confusion about the GetObjectData method (which is called during serialization) will be a method,

  • serialize both the additional data (in the GetObjectData method of AddValue) and the fields / properties of the class;
  • or just write data to the GetObjectData method without entering the fields / properties of the class?

I debugged (2) correctly - field / property data is not serialized if GetObjectData method is used? It's right? (I am not an expert and just want to confirm here, but I am 100% confident in myself.)

+3
source share
3 answers

ISerializable, (.. "2" ); . ? , DataContractSerializer, , , ( ) . ( ..), , , , protobuf-net, , .

: ?

:

[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 ....}
    }
}
+2

ISerializable, ( , , ), SerializationInfo, AddValue.

+2

I'm not sure what you want to achieve, but it's not easy to let C # do the job 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 XmlSerializer or BinaryFormatter to (de) serialize the object

+2
source

Source: https://habr.com/ru/post/1704124/


All Articles