C#是否调用反序列化构造函数而不是默认构造函数?
我刚刚开始熟悉C#中对象的序列化。我想知道是否调用了反序列化构造函数而不是默认构造函数,或者是除了。如果是附加的,这些调用的顺序是什么?例如:C#是否调用反序列化构造函数而不是默认构造函数?,c#,wpf,serialization,constructor,iserializable,C#,Wpf,Serialization,Constructor,Iserializable,我刚刚开始熟悉C#中对象的序列化。我想知道是否调用了反序列化构造函数而不是默认构造函数,或者是除了。如果是附加的,这些调用的顺序是什么?例如: [Serializable()] public class ReadCache : ISerializable { protected ArrayList notifiedURLs; // Default constructor public ReadCache() { notifiedURLs = ne
[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));
}
}
不,它将被称为“代替”,而不是默认值-但您可以使用以下内容初始化列表:
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()”语法-但在您的特殊情况下,您不必这样做 明白了。使用:this()时,首先执行哪个代码?默认构造函数代码还是反序列化构造函数代码?您的“默认”-构造函数-中的部分请仔细想想,否则不是很有用,不是吗?