I have problem with serialization. I want to convert an object into a string and vice versa. I have two utility methods:
public
static byte[] Serialize(Object o)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf1 = new BinaryFormatter();
bf1.Serialize(ms, o);
byte[] buffer = ms.ToArray();
//string retStr = Convert.ToBase64String(buffer);
return buffer;
}
public static object Deserialize(byte[] TheByteArray)
{
//byte[] TheByteArray = Convert.FromBase64String(ParamStr);
MemoryStream ms = new MemoryStream(TheByteArray);
BinaryFormatter bf1 = new BinaryFormatter();
ms.Position = 0;
return bf1.Deserialize(ms);
}
My test code is:
Student obj = new Student ();
obj.UserName = "Admin";
obj.Password = "Password";
obj.lessonIds = new int[] { 1, 2, 3, 4, 5 };
obj.lessonNames= new string[] { "Spanish", "Maths" };
obj.Id= 43;
byte[] retByteArray = Crypto.Serialize(obj);
Student objNew = new Student ();
objNew = (Student )Crypto.Deserialize(retByteArray);
this code does not work. The error message is : Exception has been thrown by the target of an invocation. End of Stream encountered before parsing was completed.
End my main aim is convert object into string but I even cannot convert it into byte array