C#嵌套类的序列化

前端之家收集整理的这篇文章主要介绍了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]
原文链接:https://www.f2er.com/csharp/244232.html

猜你在找的C#相关文章