Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you don't want or can't use <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx" rel="nofollow">BinaryFormatter</a>, you will have to deserialize the object yoursekf</p> <p>Modified example from msdn for Binary Formatter:</p> <p>Let's say you have you model:</p> <pre><code>[Serializable] //you could also make the class implement ISerializable class SomeModel { public String Name { get; set; } } </code></pre> <p>you have your NetworkStream:</p> <pre><code>NetworkStream ns; </code></pre> <p>basic example of how you'd do the serialization part:</p> <pre><code>void Serialize() { SomeModel myModel = new SomeModel() { Name = "mooo" }; // Construct a BinaryFormatter and use it to serialize the data to the stream. BinaryFormatter formatter = new BinaryFormatter(); try { formatter.Serialize(ns, myModel); } catch (SerializationException e) { throw e; } } </code></pre> <p>and as for deserializing</p> <pre><code>void Deserialize() { SomeModel myModel; try { BinaryFormatter formatter = new BinaryFormatter(); // Deserialize the object from the stream and // assign the reference to the local variable. myModel = (SomeModel) formatter.Deserialize(ns); } catch (SerializationException e) { throw e; } } </code></pre> <p>There's one thing you have to take care off. You have to know when you deserialize, you can't just throw the stream in it and get something back, you'll have to know that what you are going to read is in fact a serialized SomeModel-class. I don't know how your code looks like so I don't know if this is an issue or not and if it is how to avoid it.</p> <p>Anyway, your second choice would be to use a BinaryReader. You'd have to create an instance of you object, and read the data manually.</p> <pre><code>using(BinaryWriter writer = new BinaryWriter(ns)) { binWriter.Write(myModel.Name); } </code></pre> <p>reading:</p> <pre><code> using(BinaryReader reader = new BinaryReader(ns)) { try { SomeModel myModel = new SomeModel(); myModel.Name = binReader.ReadString(); } // If the end of the stream is reached before reading // the four data values, ignore the error and use the // default settings for the remaining values. catch(EndOfStreamException e) { Console.WriteLine("{0} caught and ignored. " + "Using default values.", e.GetType().Name); } } </code></pre>
    singulars
    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