Issue regarding Serialization [modified]
-
Hi, I am facing an issue in serialization. I have a class that implement ISerializable interface. Actually I want to make my object to travel on the network. If I don’t implemetn the ISerializable interfce and use the following code I can get the same functionality.
public static Object GetSerializedObject (Object pObject)
{
try
{
BinaryFormatter objBinaryFormatter = new BinaryFormatter();
MemoryStream objMemoryStream = new MemoryStream();
objBinaryFormatter = new BinaryFormatter();
objBinaryFormatter.Serialize(objMemoryStream, pObject);
objMemoryStream.Position = 0;
Object objReturnedObject = (object)objBinaryFormatter.Deserialize(objMemoryStream);
objMemoryStream.Flush();
objMemoryStream.Close();
objBinaryFormatter = null;
return objReturnedObject;} catch (Exception objException) { throw; }
}
Please guide me which is the best practice that I should follow. Is there any difference in the approches. There is no constrant to serialize some attribute of the class, the whole object should be serialized. Regards,
modified on Tuesday, February 26, 2008 8:11 AM
-
Hi, I am facing an issue in serialization. I have a class that implement ISerializable interface. Actually I want to make my object to travel on the network. If I don’t implemetn the ISerializable interfce and use the following code I can get the same functionality.
public static Object GetSerializedObject (Object pObject)
{
try
{
BinaryFormatter objBinaryFormatter = new BinaryFormatter();
MemoryStream objMemoryStream = new MemoryStream();
objBinaryFormatter = new BinaryFormatter();
objBinaryFormatter.Serialize(objMemoryStream, pObject);
objMemoryStream.Position = 0;
Object objReturnedObject = (object)objBinaryFormatter.Deserialize(objMemoryStream);
objMemoryStream.Flush();
objMemoryStream.Close();
objBinaryFormatter = null;
return objReturnedObject;} catch (Exception objException) { throw; }
}
Please guide me which is the best practice that I should follow. Is there any difference in the approches. There is no constrant to serialize some attribute of the class, the whole object should be serialized. Regards,
modified on Tuesday, February 26, 2008 8:11 AM
You don't need to implement the ISerializable interface. Just decorate your class with the [Serializable] attribute and you are fine.