andruhovski icon

Serialize

andruhovski | PRO | 09/11/18 09:39:24 AM UTC | 0 ⭐ | 541 👁️ | Never ⏰ | []
C# |

1.69 KB

|

None

|

0 👍

/

0 👎

 [Serializable]
    class Student : ISerializable
    {
        public string LastName;
        public string FirstName;
        public DateTime birthDate;
        public bool isSelfPayer;
        public double avgScore;
        public Student(SerializationInfo info, StreamingContext context)
        {
            LastName = info.GetString("LastName");
            FirstName = info.GetString("FirstName");
            birthDate = (DateTime)info.GetValue("birthDate", birthDate.GetType());
            isSelfPayer = info.GetBoolean("isSelfPayer");
            avgScore = info.GetDouble("avgScore");
        }
 
        public Student()
        {
            LastName = string.Empty;
            FirstName = string.Empty;
            birthDate = DateTime.Today;
            isSelfPayer = false;
            avgScore = 0.0;
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("LastName", LastName);
            info.AddValue("FirstName", FirstName);
            info.AddValue("birthDate", birthDate);
            info.AddValue("isSelfPayer", isSelfPayer);
            info.AddValue("avgScore", avgScore);
        }
    }
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            var student = new Student();
            student.FirstName = "Антон";
            student.LastName = "Антонов";
            student.avgScore = 3.4;
            IFormatter formatter = new SoapFormatter();
            FileStream buffer = File.Create("D:\\ABC\\student.xml");
            formatter.Serialize(buffer, student);
            buffer.Close();
        }
    }

Comments