|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article describes how to serialize an object which is not marked as 'Serializable' using a surrogate. BackgroundSerialization is a process of converting an object in memory into a byte stream, suitable for transportation across the wire and persisting in a stable storage. Only an object that is marked as serializable can be serialized. The problem here is to serialize an object which is not marked as serializable. Using the codeWe will look into a step-by-step approach of "How to serialize an object which is not marked as 'Serializable' using a surrogate". For this purpose, consider a simple class called The /// <SUMMARY>
/// NOTE: It is assumed for this demo that the source code for this class
/// is not available and this class cannot be marked as '[Serializable]'.
/// </SUMMARY>
public class Vehicle
{
/// <SUMMARY>
/// Make of the vehicle.
/// </SUMMARY>
private string _make;
/// <SUMMARY>
/// Model of the vehicle.
/// </SUMMARY>
private string _model;
public string Make
{
get
{
return _make;
}
set
{
_make = value;
}
}
public string Model
{
get
{
return _model;
}
set
{
_model = value;
}
}
}
In order to serialize this class, a new class called public class VehicleSurrogate : ISerializationSurrogate
{
/// This method is part of the interface ISerializationSurrogate
public void GetObjectData(object obj, SerializationInfo info,
StreamingContext context)
{
Vehicle vehicle = obj as Vehicle;
// Set the vehicle object's properties
info.AddValue("Make", vehicle.Make);
info.AddValue("Model", vehicle.Model);
}
/// This method is part of the interface ISerializationSurrogate
public object SetObjectData(object obj, SerializationInfo info,
StreamingContext context, ISurrogateSelector selector)
{
// Restore the vehicle object's properties
Vehicle vehicle = obj as Vehicle;
vehicle.Make = info.GetString("Make");
vehicle.Model = info.GetString("Model");
// This is ignored.
return null;
}
The interface public void SerializeUsingSurrogate()
{
try
{
// Create a new instance of the vehicle class.
// NOTE: It is assuming for this demo that the source
// code for this class
// is not available.
Vehicle vehicle = new Vehicle();
vehicle.Make = "Cadillac";
vehicle.Model = "2005";
// 1. Create an instance of a Soap Formatter.
IFormatter formatter = new SoapFormatter();
// 2. Construct a SurrogateSelector object
SurrogateSelector ss = new SurrogateSelector();
// 3. Construct an instance of our serialization surrogate type
VehicleSurrogate vss = new VehicleSurrogate();
// 4. Tell the surrogate selector to use our object when a
// Vehicle object is serialized/deserialized.
// NOTE: AddSurrogate can be called multiple times to register
// more types with their associated surrogate types
ss.AddSurrogate(typeof(Vehicle),
new StreamingContext(StreamingContextStates.All), vss);
// Creating a byte array to hold the data.
byte[] bData = new byte[1024];
MemoryStream memoryStream = new MemoryStream(bData);
TextWriter textWriter = new StreamWriter(@"C:\Vehicle.xml");
// 5. Have the formatter use our surrogate selector
formatter.SurrogateSelector = ss;
// Try to serialize a Vehicle object
formatter.Serialize(memoryStream, vehicle);
// Create a buffer to hold the data.
StringBuilder buffer = new StringBuilder();
for(int count = 0; count < bData.Length; count++)
{
if ( bData[count] != '\0')
{
// Convert the char stream data to string.
buffer.Append( Convert.ToChar(bData[count]));
}
}
textWriter.WriteLine(buffer.ToString() );
// clean up.
textWriter.Close();
buffer = null;
}
catch(Exception)
{
// Check for exception in case of a fault.
}
}
Let us walkthrough this code step-by-step...
Points of InterestA '
|
||||||||||||||||||||||