Unless you mark the class with the
[Serializable] attribute, this
interface must be implemented by all
classes with serialized instances. Use
the ISerializable interface if you
want your class to control its own
serialization and de-serialization.
The GetObjectData() let you control the serialization process.
GetDataObject, to which you pass a
SerializationInfo object and a
StreamingContext object. The
GetDataObject method will then
populate the SerializationInfo object
with the data necessary for the
serialization of the target object.
Example:
public Employee(SerializationInfo info, StreamingContext ctxt)
{
//Get the values from info and assign them to the appropriate properties
EmpId = (int)info.GetValue("EmployeeId", typeof(int));
EmpName = (String)info.GetValue("EmployeeName", typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
//You can use any custom name for your name-value pair. But make sure you
// read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
// then you should read the same with "EmployeeId"
info.AddValue("EmployeeId", EmpId);
info.AddValue("EmployeeName", EmpName);
}
The example above show you how to deserialize and serialize. As you can see GetObjectData is required to deserialize. If you leave it empty, your object won't have the desire values.