2012-03-12 84 views
1

我剛剛熟悉C#中對象的序列化。我想知道如果反序列化構造函數被稱爲INSTEAD的默認構造函數或IN ADDITION TO。如果它是IN ADDITION TO,這些調用的順序是什麼?例如:C#反序列化構造函數是否調用默認構造函數的INSTEAD?

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

回答

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

請注意,「...:此()「 - 語法 - 但在你的特殊情況下,你不必!

+0

Gotcha。何時:使用this()時,哪個代碼先執行?默認的構造函數代碼或反序列化構造函數代碼? – Doug 2012-03-12 06:56:24

+1

部分在你的「默認」 - 構造 - 想一想,不是很有用,否則不是嗎? – Carsten 2012-03-12 06:59:46

+0

非常感謝。 – Doug 2012-03-12 07:01:08