How I can make Student class serializable? I’m reading this article but I do not know what is the right approach if I would implement it in the below scenario.
public class Student
{
private string _studentNumber;
private string _lastName;
private string _firtName;
private List<Subject> _subjects;
public Student() { }
public string StudentNumber
{
get { return _studentNumber; }
set { _studentNumber = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public string FirstName
{
get { return _firtName; }
set { _firtName = value; }
}
public List<Subject> Subjects
{
get { return _subjects; }
set { _subjects = value; }
}
}
public class Subject
{
private string _subjectCode;
private string _subjectName;
public Subject() { }
public string SubjectCode
{
get { return _subjectCode; }
set { _subjectCode = value; }
}
public string SubjectName
{
get { return _subjectName; }
set { _subjectName = value; }
}
}
For
BinaryFormatter, just add[Serializable]before thepublic class ....For
XmlSerializer, nothing; that should work fine assuming you want elementsFor
DataContractSerializer(a 3.0 class, strictly speaking), add[DataContract]to the class, and[DataMember]to the propertiesFor protobuf-net, you can use the
DataContractSerializerattributes as long as each[DataMember]specifies a uniqueOrder=, or you can use[ProtoContract]on the class, and[ProtoMember(n)]on each property, for uniquen.For
JavaScriptSerializer(technically 3.5), nothing – it should workFor
Json.NET, nothing – it should workOther options would include implementing
ISerializableorIXmlSerializable, but frankly: you don’t need that.