C#将反序列化构造函数调用为默认构造函数的INSTEAD

本文关键字:默认 构造函数 INSTEAD 函数调用 反序列化 | 更新日期: 2023-09-27 18:27:23

我刚刚熟悉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));
    }
}

C#将反序列化构造函数调用为默认构造函数的INSTEAD

不,它将被称为"而不是"默认值,但您可以用这样的东西初始化您的列表:

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()"语法-但在您的特殊情况下,您不必这样做!