Version Control for serialization and deserialization, serialization and deserialization of collection objects, and serialization of Sets

Source: Internet
Author: User

Version Control for serialization and deserialization, serialization and deserialization of collection objects, and serialization of Sets

When cross-process or even cross-domain data transmission is involved, we need to serialize and deserialize the object.

 

First, you can use the Serializable feature.

 

    [Serializable]
    public class Person
    {
        public string _firstName;
        public string _secondName;
// Serialization
        [OnSerializing]
        internal void OnSerializing(StreamingContext context)
        {
            _firstName = _firstName.ToUpper();
            _secondName = _secondName.ToUpper();
        }
// Deserialization
        [OnDeserialized]
        internal void OnDeserialized(StreamingContext context)
        {
            _firstName = _firstName.ToLower();
            _secondName = _secondName.ToLower();
        }
    }

 

Of course, you can also implement the ISerializable interface. Complete serialization in the interface method GetObjectData, save the information to SerializationInfo, deserialize the information in the constructor, and read the information from SerializationInfo.

 

   [Serializable]
    public class Person : ISerializable
    {
        public string _firstName;
        public string _secondName;
        public Person()
        {
            
        }
// Deserialization
        public Person(SerializationInfo si, StreamingContext context)
        {
            _firstName = si.GetString("first").ToLower();
            _secondName = si.GetString("second").ToLower();
        }
// Serialization
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("first", _firstName.ToUpper());
            info.AddValue("second",_secondName.ToUpper());
        }
    }

 

□Version control of serialized and deserialized objects

 

The following class describes the first version of the object.

 

[Serializable]
public class SomeClass : ISerializable
{
    private int a;
    public SomeClass(){}
    public SomeClass(SerializationInfo info, StreamingContext context)
    {
        a = info.GetInt32("myval");
    }
    public void GetObjectData(SerializationInfo, StreamingContext context)
    {
        info.AddValue("VERSION",1);
         info.AddValue("myval", a);
    }
}    

 

Now, a private field is added to SomeClass to become version 2.

 

[Serializable]
public class SomeClass : ISerializable
{
    private int a;
    private string b;
    public SomeClass(){}
    public SomeClass(SerializationInfo info, StreamingContext context)
    {
        int VERSION = info.GetInt32("VERSION");
        a = info.GetInt32("a");
        if(VERSION > 1)
        {
            b = info.GetString("another");
        }
        else
        {
B = "Default Value ";
        }
    }
    public void GetObjectData(SerializationInfo, StreamingContext context)
    {
        info.AddValue("VERSION",2);
         info.AddValue("myval", a);
         info.AddValue("another", b);
    }
}  

 

□Serialize the collection object to a file and read it through deserialization

 

If a collection object needs to be serialized and deserialized, the collection element object must be serializable and deserialized, and the object attributes in the collection element object must also be serializable and deserialized, and so on.

 

    [Serializable]
    public class Car : ISerializable
    {
        private string _model;
        private int _year;
        private Owner _owner;
        public Car()
        {
            
        }
        public Car(SerializationInfo info, StreamingContext context)
        {
            this._model = (string) info.GetValue("Model", typeof (string));
            this._year = (int) info.GetValue("Year", typeof (int));
            this._owner = (Owner) info.GetValue("Owner", typeof (Owner));
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Model",this._model);
            info.AddValue("Year", this._year);
            info.AddValue("Owner", this._owner);
        }
    }
    [Serializable]
    public class Owner : ISerializable
    {
        private string _name;
        public Owner()
        {
            
        }
        public Owner(SerializationInfo info, StreamingContext context)
        {
            this._name = (string) info.GetValue("Name", typeof (string));
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Name", this._name);
        }
    }

 

Above, we want to serialize the Car set to the file, and then deserialize the Car set to read it. We must enable the Car to be serialized and deserialized, and the Owner of the Car property object must also be serializable and deserialized.

 

Next, it is used to encapsulate a class of the Car set.

 

    [Serializable]
    public class CarsList : ISerializable
    {
        private List<Car> _cars;
        public List<Car> Cars
        {
            get { return this._cars; }
            set { this._cars = value; }
        }
        public CarsList()
        {
            
        }
        public CarsList(SerializationInfo info, StreamingContext context)
        {
            this._cars = (List<Car>) info.GetValue("Cars", typeof (List<Car>));
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Cars", this._cars);
        }
    }

 

Write a help class for serialization and deserialization for CarsList.

 

    public class SerializerHelper
    {
        public SerializerHelper()
        {
            
        }
// Serialization
        public void SerializeObject(string fileName, CarsList carsList)
        {
// Open a stream based on the file name
            Stream stream = File.Open(fileName, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
// Serialize the object to the stream
            bFormatter.Serialize(stream,carsList);
            stream.Close();
        }
// Deserialization
        public CarsList DeserializeObject(string fileName)
        {
            CarsList carsList;
// Open a stream based on the file name
            Stream stream = File.Open(fileName, FileMode.Open);
            BinaryFormatter bfFormatter = new BinaryFormatter();
            carsList = (CarsList)bfFormatter.Deserialize(stream);
            stream.Close();
            return carsList;
        }
    }    

 

Call the following command on the client:

 

        static void Main(string[] args)
        {
            List<Car> cars = new List<Car>();
            CarsList carsList = new CarsList();
            carsList.Cars = cars;
            SerializerHelper serializerHelper = new SerializerHelper();
            serializerHelper.SerializeObject(@"temp.txt", carsList);
            carsList = serializerHelper.DeserializeObject(@"temp.txt");
            cars = carsList.Cars;
        }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.