serialization/person-4/person.cs - The Person class - Serialization control with attributes. | Lecture 10 - slide 33 : 40 Program 1 |
using System; using System.Runtime.Serialization; [Serializable] public class Person{ private string name; [NonSerialized()] private int age; private Date dateOfBirth, dateOfDeath; public Person (string name, Date dateOfBirth){ this.name = name; this.dateOfBirth = dateOfBirth; this.dateOfDeath = null; age = Date.Today.YearDiff(dateOfBirth); } [OnDeserialized()] internal void FixPersonAfterDeserializing( StreamingContext context){ age = Date.Today.YearDiff(dateOfBirth); } public Date GetDateOfBirth(){ return new Date(dateOfBirth); } public int Age{ get {return Alive ? age : dateOfDeath.YearDiff(dateOfBirth);} } public bool Alive{ get {return dateOfDeath == null;} } public void Died(Date d){ dateOfDeath = d; } public void Update(){ age = Date.Today.YearDiff(dateOfBirth); } public override string ToString(){ return "Person: " + name + " *" + dateOfBirth + (Alive ? "" : " +" + dateOfDeath) + " Age: " + age; } }