3

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; }
    }
}
Community
  • 1
  • 1
yonan2236
  • 13,371
  • 33
  • 95
  • 141
  • 3
    serializable **by which serializer** ? – Marc Gravell May 08 '12 at 06:03
  • Also, if you are using the C# 3 compiler, but targeting .NET 2.0, you can use automatically implemented properties, saving some typing - **but** note: that is a breaking change for `BinaryFormatter` (old data will not be compatible if you change to automatically implemented properties) – Marc Gravell May 08 '12 at 06:09
  • sorry, not quite familiar to this but I'm refering to this article. Seems the same with my question: http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document – yonan2236 May 08 '12 at 06:10

2 Answers2

9

For BinaryFormatter, just add [Serializable] before the public class ....

For XmlSerializer, nothing; that should work fine assuming you want elements

For DataContractSerializer (a 3.0 class, strictly speaking), add [DataContract] to the class, and [DataMember] to the properties

For protobuf-net, you can use the DataContractSerializer attributes as long as each [DataMember] specifies a unique Order=, or you can use [ProtoContract] on the class, and [ProtoMember(n)] on each property, for unique n.

For JavaScriptSerializer (technically 3.5), nothing - it should work

For Json.NET, nothing - it should work

Other options would include implementing ISerializable or IXmlSerializable, but frankly: you don't need that.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

Usually i serialize to make a deep copy of my object without implementing IClonenable interface.

In your case you can use

[Serializable()]
public class Student
{
   // Your Class
}

make a extension method

public static class ExtensionMethod
{  
    public static T DeepClone<T>(this T element)
    { 
        MemoryStream m = new MemoryStream();
        BinaryFormatter b = new BinaryFormatter();
        b.Serialize(m, element);
        m.Position = 0;
        return (T)b.Deserialize(m);
    }
}

and call it like

yourclassobject.DeepClone();
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208