C#嵌套类的序列化

我想序列化一个对象.我有这个基本的类结构:
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);
            }
        }
    }
}

Clock类的定义如下:

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

基础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);
            }
        }
    }


}

当我尝试使用包含的ClockProperties数组序列化Clock对象时,我得到一个异常,即Controller未标记为可序列化.老实说,我不明白为什么.我假设我只序列化Clockobject,因此只将该类和ClockProperties标记为Serializable.我错过了什么吗?

解决方法

在类Clock中标记ClockChangedEvent [field:NonSerialized]

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...