2009-12-14 100 views
6

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

时钟类是这样定义的:

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


} 

当我尝试序列化对象Clock与包括阵列ClockProperties,我得到一个异常,Controller未标记为可序列化。老实说,我不明白为什么。我假设我只序列化了Clock对象,因此只标记该类,而ClockProperties标记为Serializable。我错过了什么吗?

回答

8

在课堂时钟标志的ClockChangedEvent [字段:非序列化]

+0

谢谢,我试过'[NonSerialized]',因为我偶然发现了它。不知道该语法'[field:NonSerialized]'。它现在按预期工作。 – rdoubleui 2009-12-14 13:36:11

+0

不错,不知道那个。 – Svish 2009-12-14 13:43:06

2

时钟上的事件可能是您的问题,因为它是对控制器的引用。 That's a known "issue"

你需要使事件或其后台字段不可序列化,你应该没问题。

+1

我试过了,但是这是缺少对我来说一下,并没有涉及该文章。正如Henrik所建议的那样,对于事件欺骗,需要声明'[field:NonSerialized]'。 – rdoubleui 2009-12-14 13:35:06