簡體   English   中英

C#嵌套類的序列化

[英]C# Serialization of nested classes

我想序列化一個對象。 我有這個基本的類結構:

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未標記為可序列化。 老實說,我不明白為什么。 我假設我只序列化Clock對象,因此只將該類和ClockProperties標記為Serializable 我錯過了什么嗎?

在類Clock中標記ClockChangedEvent [field:NonSerialized]

時鍾上的事件可能是您的問題,因為它是對Controller的引用。 這是一個眾所周知的“問題”

您需要使事件或其支持字段不可序列化,您應該沒問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM