views:

196

answers:

2

Hi,

I want to serialize an object. I've got this basic class structure:

class Controller
{        
    Clock clock;        

    public event EventHandler<ClockChangedEventArgs> 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<ClockChangedEventArgs>(clock_ClockChanged);
            }
        }
    }
}

The Clock class is defined like this:

[Serializable]
public class Clock 
{
    ClockProperties[] properties;
    int current;
    bool isAnimated;

    public event EventHandler<ClockChangedEventArgs> 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);
    }
}

The underlying ClockProperties:

[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);
            }
        }
    }


}

When I try to serialize the Clock object with the included Array of ClockProperties, I get an exception that the Controller is not marked serializable. Honestly, I don't understand why. I assumed I only serialize the Clockobject, and therefore only mark that class and the ClockProperties as Serializable. Am I missing something?

+2  A: 

The event on the clock is likely your problem, since it is a reference to the Controller. That's a known "issue".

You need to make the event or its backing field non-serializable, and you should be fine.

Lucero
I tried that, however the bit that was missing for me, was not covered in that article. For event declerations one needs to state `[field:NonSerialized]`, as Henrik suggested.
rdoubleui
+5  A: 

In class Clock mark the ClockChangedEvent [field:NonSerialized]

Henrik
Thank you, I tried `[NonSerialized]`, as I stumbled upon it. Didn't know about that syntax `[field:NonSerialized]`. It works now as intended.
rdoubleui
Nice, didn't know about that one.
Svish