1

我刚刚熟悉 C# 中对象的序列化。我想知道反序列化构造函数是否被称为 INSTEAD OF 默认构造函数或 IN ADDITION TO。如果是 IN ADDITION TO,这些调用的顺序是什么?例如:

[Serializable()]
public class ReadCache : ISerializable
{
    protected ArrayList notifiedURLs;

    // Default constructor
    public ReadCache()
    {
        notifiedURLs = new ArrayList();
    }

    // Deserialization constructor.
    public ReadCache(SerializationInfo info, StreamingContext ctxt)
    {
        //Get the values from info and assign them to the appropriate properties
        notifiedURLs = (ArrayList)info.GetValue("notifiedURLs", typeof(ArrayList));
    }
}
4

1 回答 1

2

不,它将被称为“而不是”默认值 - 但您可以使用以下内容初始化列表:

public ReadCache(SerializationInfo info, StreamingContext ctxt)
  : this()
{
    //Get the values from info and assign them to the appropriate properties
    notifiedURLs = (ArrayList)info.GetValue("notifiedURLs", typeof(ArrayList));
}

请注意“ ... : this() ” - 语法 - 但在您的特殊情况下,您不必这样做!

于 2012-03-12T06:47:04.797 回答