Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Serialization of nested classes
    primarykey
    data
    text
    <p>I want to serialize an object. I've got this basic class structure:</p> <pre><code>class Controller { Clock clock; public event EventHandler&lt;ClockChangedEventArgs&gt; ClockChanged; public void serializeProperties() { using (FileStream stream = new FileStream(PROPERTIES_FILE, FileMode.Append, FileAccess.Write, FileShare.Write)) { IFormatter formatter = new BinaryFormatter(); try { formatter.Serialize(stream, clock); } catch (Exception e) { Console.WriteLine(e.Message); } } } public void deserializeProperties() { using (FileStream stream = new FileStream(PROPERTIES_FILE, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)) { IFormatter formatter = new BinaryFormatter(); try { clock = (Clock)formatter.Deserialize(stream); } catch (Exception e) { Console.WriteLine(e.Message); clock = new Clock(); } finally { clock.ClockChanged += new EventHandler&lt;ClockChangedEventArgs&gt;(clock_ClockChanged); } } } } </code></pre> <p>The Clock class is defined like this: </p> <pre><code>[Serializable] public class Clock { ClockProperties[] properties; int current; bool isAnimated; public event EventHandler&lt;ClockChangedEventArgs&gt; ClockChanged; public Clock() { properties = new ClockProperties[2]; properties[0] = new ClockProperties("t1"); properties[1] = new ClockProperties("t2"); properties[0].ValueChanged += new EventHandler(Clock_ValueChanged); properties[1].ValueChanged += new EventHandler(Clock_ValueChanged); } } </code></pre> <p>The underlying ClockProperties:</p> <pre><code>[Serializable] public class ClockProperties { public event EventHandler ValueChanged; int posX, posY; string clock; public ClockProperties(string name) { clock = name; } public void OnValueChanged(EventArgs e) { if (ValueChanged != null) { ValueChanged(this, e); } } public string Clock { get { return clock; } set { if (!value.Equals(clock)) { clock = value; OnValueChanged(EventArgs.Empty); } } } public int PosX { get { return posX; } set { if (!(value == posX)) { posX = value; OnValueChanged(EventArgs.Empty); } } } public int PosY { get { return posY; } set { if (!(value == posY)) { posY = value; OnValueChanged(EventArgs.Empty); } } } } </code></pre> <p>When I try to serialize the <code>Clock</code> object with the included Array of <code>ClockProperties</code>, I get an exception that the <code>Controller</code> is not marked serializable. Honestly, I don't understand why. I assumed I only serialize the <code>Clock</code>object, and therefore only mark that class and the <code>ClockProperties</code> as <code>Serializable</code>. Am I missing something?</p>
    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.
 

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