C#反序列化构造函数是否被称为默认构造函数的INSTEAD?

时间:2012-03-12 06:42:03

标签: c# wpf serialization constructor iserializable

我只是熟悉C#中对象的序列化。我想知道反序列化构造函数是否被调用INSTEAD OF默认构造函数或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));
    }
}

1 个答案:

答案 0 :(得分: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()” - 语法 - 但在您的特殊情况下,您不必这样做!