简体   繁体   English

C#嵌套类的序列化

[英]C# Serialization of nested classes

I want to serialize an object. 我想序列化一个对象。 I've got this basic class structure: 我有这个基本的类结构:

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

The Clock class is defined like this: 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);
    }
}

The underlying ClockProperties: 基础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);
            }
        }
    }


}

When I try to serialize the Clock object with the included Array of ClockProperties , I get an exception that the Controller is not marked serializable. 当我尝试使用包含的ClockProperties数组序列化Clock对象时,我得到一个异常,即Controller未标记为可序列化。 Honestly, I don't understand why. 老实说,我不明白为什么。 I assumed I only serialize the Clock object, and therefore only mark that class and the ClockProperties as Serializable . 我假设我只序列化Clock对象,因此只将该类和ClockProperties标记为Serializable Am I missing something? 我错过了什么吗?

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

The event on the clock is likely your problem, since it is a reference to the Controller. 时钟上的事件可能是您的问题,因为它是对Controller的引用。 That's a known "issue" . 这是一个众所周知的“问题”

You need to make the event or its backing field non-serializable, and you should be fine. 您需要使事件或其支持字段不可序列化,您应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM