Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat mistake am I making when serializing?
    text
    copied!<p>This leads to serialization exception at runtime. It's just a demo project to test the best way to do this. I included the main method and the class which im trying to serialize.</p> <p>Ignore: I really cant add more detail, i've described the problem, attached the code, this "please add more details" thing is the stupidest thing ever. Let me post it already.</p> <pre><code>Data toSend = new Data(); toSend.Output(); ///SERIALIZE BinaryFormatter formatter = new BinaryFormatter(); Stream streamOut = File.OpenWrite("file"); formatter.Serialize(streamOut, toSend); streamOut.Close(); Console.WriteLine("----------------------------"); ///DESERIALIZE Stream streamIn = File.OpenRead("file"); Object received = formatter.Deserialize(streamIn); Data toReceive = (Data)received; toReceive.Output(); class Data : ISerializable { int integerData; string stringData; bool booleanData; int shouldnotbeserialized; public Data() { integerData = 1; stringData = "Hello"; booleanData = true; shouldnotbeserialized = 55; } //To deserialize public Data(SerializationInfo info, StreamingContext context) { integerData = info.GetInt32("theint"); stringData = info.GetString("thestring"); booleanData = info.GetBoolean("thebool"); } public void Output() { Console.WriteLine(integerData); Console.WriteLine(stringData); Console.WriteLine(booleanData); Console.WriteLine(shouldnotbeserialized); } //Implemented method to serialize public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("thestring", stringData); info.AddValue("theint", integerData); info.AddValue("thebool", booleanData); } } </code></pre> <p>Exception message:</p> <blockquote> <p>Type 'SerializationDemo.Data' in Assembly 'SerializationDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.</p> </blockquote>
 

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