How to do it...

  1. Create a class called Vehicle. You will notice that this class implements ISerializable while still having the [Serializable] attribute. You must do this so that the Common Language Runtime can identify this class as serializable:
        [Serializable]
public class Vehicle : ISerializable
{

}
  1. To this class, add the following fields and the constructor:
        // Primitive fields
public int VehicleType;
public int EngineCapacity;
public int TopSpeed;

public Vehicle()
{

}
  1. When you implement ISerilizable on your Vehicle class, Visual Studio will alert you that the ISerializable interface has not been implemented inside your class. Add the implementation by clicking on the lightbulb next to the underlined interface name and accepting the correction. Visual Studio will now add the GetObjectData() method inside your class. Note that the method is added with an exception that will throw a NotImplementedException if you don't add some code to the method. Add very basic code here that simply adds the values of the fields to the SerializationInfo object:
        public void GetObjectData(SerializationInfo info, 
StreamingContext context)
{
info.AddValue("VehicleType", VehicleType);
info.AddValue("EngineCapacity", EngineCapacity);
info.AddValue("TopSpeed", TopSpeed);
}
  1. As mentioned previously, we need to add the deserialization constructor that will deserialize the fields. This, you add manually:
        // Deserialization constructor
protected Vehicle(SerializationInfo info, StreamingContext context)
{
VehicleType = info.GetInt32("VehicleType");
EngineCapacity = info.GetInt32("EngineCapacity");
TopSpeed = info.GetInt32("TopSpeed");
}
  1. After adding all the code, your class should look as follows:
        [Serializable]
public class Vehicle : ISerializable
{
// Primitive fields
public int VehicleType;
public int EngineCapacity;
public int TopSpeed;

public Vehicle()
{

}
public void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("VehicleType", VehicleType);
info.AddValue("EngineCapacity", EngineCapacity);
info.AddValue("TopSpeed", TopSpeed);
}

// Deserialization constructor
protected Vehicle(SerializationInfo info,
StreamingContext context)
{
VehicleType = info.GetInt32("VehicleType");
EngineCapacity = info.GetInt32("EngineCapacity");
TopSpeed = info.GetInt32("TopSpeed");
}
}
  1. We are simply going to write the serialized class to a file. For the purposes of this recipe, simply hardcode an output path for the file. Next, create a new instance of the Vehicle class and set some values to the fields:
        string serializationPath = @"C:	empvehicleInfo.dat";
Vehicle vehicle = new Vehicle();
vehicle.VehicleType = (int)VehicleTypes.Car;
vehicle.EngineCapacity = 1600;
vehicle.TopSpeed = 230;

if (File.Exists(serializationPath))
File.Delete(serializationPath);
  1. Also be sure to add the VehicleTypes enumerator to the top of your class:
        public enum VehicleTypes
{
Car = 1,
SUV = 2,
Utility = 3
}
  1. We then add the code that will serialize the class to the file you specified in the hardcoded path. To do this, we add a FileStream and a BinaryFormatter object to serialize the vehicle to the file:
        using (FileStream stream = new FileStream(serializationPath, 
FileMode.Create))
{
BinaryFormatter fmter = new BinaryFormatter();
fmter.Serialize(stream, vehicle);
}
  1. Lastly, we add the code to read the file containing the serialized data and create the Vehicle object containing the state of the Vehicle at the time it was serialized. While the deserialize code runs immediately after the serialize code, note that this is just for demonstration purposes. The Vehicle deserialization could occur at any future point in time by reading from the file:
        using (FileStream stream = new FileStream(serializationPath, 
FileMode.Open))
{
BinaryFormatter fmter = new BinaryFormatter();
Vehicle deserializedVehicle = (Vehicle)fmter.Deserialize(stream);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.190.211