Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That code works perfectly for me (I simply added the missing <code>Student</code> class. Is that code actually representative of your <em>real</em> code. In particular, buffer handling (maybe file IO) would be my first suspicion. But the bug isn't in the code you posted... it works fine.</p> <p>As an aside... <code>BinaryFormatter</code> can be brittle, especially around different versions - you may want to consider alternative serializers. Ask for more info if you are interested.</p> <p>Here it is runnable:</p> <pre><code>using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System; static class Crypto { 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); } [Serializable] class Student { public string UserName { get; set; } public string Password { get; set; } public int[] LessonIds { get; set; } public string[] LessonNames { get; set; } public int Id { get; set; } } static void Main() { 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 = (Student)Crypto.Deserialize(retByteArray); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload