嵌套类的C#序列化

嵌套类的C#序列化,c#,.net,serialization,C#,.net,Serialization,我想序列化一个对象。我有一个基本的班级结构: class Controller { Clock clock; public event EventHandler<ClockChangedEventArgs> ClockChanged; public void serializeProperties() { using (FileStream stream = new FileStream(P

我想序列化一个对象。我有一个基本的班级结构:

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

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

时钟上的事件可能是您的问题,因为它是对控制器的引用


您需要使事件或其支持字段不可序列化,您应该会没事。

在类时钟标记ClockChangedEvent[field:NonSerialized]

我尝试过,但是那篇文章没有涉及到我缺少的部分。如Henrik所建议的,对于事件偏差,需要声明
[字段:非序列化]
。谢谢,我无意中尝试了
[非序列化]
。不知道该语法
[字段:非序列化]
。它现在可以正常工作了。很好,我不知道那个。
[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);
    }
}
[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);
            }
        }
    }


}