2013-12-16 110 views
0

我使用Dr.WPF的ObservableDictionary類,我需要序列化和反序列化這個集合。序列化進展順利,但是當我嘗試反序列化拋出異常:無法反序列化ObservableDictionary

$exception {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException} 
StackTrace " at EngineConfigurationManager.ObservableDictionary.ObservableDictionary`2.AddEntry(TKey key, TValue value) in d:\\Доки\\Editor\\EngineConfigurationManager\\EngineConfigurationManager\\ObservableDictionary\\ObservableDictionary.cs:line 196\r\n at EngineConfigurationManager.ObservableDictionary.ObservableDictionary`2.OnDeserialization(Object sender) in d:\\Доки\\Editor\\EngineConfigurationManager\\EngineConfigurationManager\\ObservableDictionary\\ObservableDictionary.cs:line 592\r\n at System.Runtime.Serialization.ObjectManager.RaiseDeserializationEvent()\r\n at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)\r\n at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)\r\n at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)\r\n at EngineConfigurationManager.ConfigClasses.Camera.CameraConfigurationManager.Load() in d:\\Доки\\Editor\\EngineConfigurationManager\\EngineConfigurationManager\\ConfigClasses\\Camera\\CameraConfigurationManager.cs:line 936" string 

我試着調試提到的方法

public virtual void OnDeserialization(object sender) 
    { 
     if (_siInfo != null) 
     { 
      Collection<DictionaryEntry> entries = (Collection<DictionaryEntry>) 
       _siInfo.GetValue("entries", typeof(Collection<DictionaryEntry>)); 
      foreach (DictionaryEntry entry in entries) 
       AddEntry((TKey)entry.Key, (TValue)entry.Value); 
     } 
    } 

protected KeyedDictionaryEntryCollection<TKey> _keyedEntryCollection; 

protected virtual bool AddEntry(TKey key, TValue value) 
    { 
     _keyedEntryCollection.Add(new DictionaryEntry(key, value)); 
     return true; 
    } 

,但我無法找到任何錯誤:鍵和值不爲空或空。

有誰知道問題的原因是什麼?

編輯: ObservableDictionary類中有幾個構造函數,但我認爲反序列化不會調用它們中的任何一個。

public ObservableDictionary() 
    { 
     _keyedEntryCollection = new KeyedDictionaryEntryCollection<TKey>(); 
    } 

    public ObservableDictionary(IDictionary<TKey, TValue> dictionary) 
    { 
     _keyedEntryCollection = new KeyedDictionaryEntryCollection<TKey>(); 

     foreach (KeyValuePair<TKey, TValue> entry in dictionary) 
      DoAddEntry((TKey)entry.Key, (TValue)entry.Value); 
    } 

    public ObservableDictionary(IEqualityComparer<TKey> comparer) 
    { 
     _keyedEntryCollection = new KeyedDictionaryEntryCollection<TKey>(comparer); 
    } 

    public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) 
    { 
     _keyedEntryCollection = new KeyedDictionaryEntryCollection<TKey>(comparer); 

     foreach (KeyValuePair<TKey, TValue> entry in dictionary) 
      DoAddEntry((TKey)entry.Key, (TValue)entry.Value); 
    } 

我可以這樣:

protected KeyedDictionaryEntryCollection<TKey> _keyedEntryCollection = new  KeyedDictionaryEntryCollection<TKey>(); 

,但我不`噸認爲這是正確的做法。 我以這種方式反序列化集合: CombinedCameraContainer =(ObservableDictionary)binaryFormatter.Deserialize(fs);

在這種情況下你有什麼建議嗎?

+0

正如我所看到的問題是在'AddEntry'中,而不是在OnDeserialization。郵政編碼的AddEntry。 – Tony

+0

這是AddEntry的代碼。我編輯後 – Denis

+0

異常堆棧跟蹤將錯誤指向「ObservableDictionary.cs」行196.當您得到異常時,在該行代碼中使用的引用爲「null」。 –

回答

1

根據您的意見,這是拋出異常的行:

_keyedEntryCollection.Add(new DictionaryEntry(key, value)); 

是在該行取消引用的唯一引用是_keyedEntryCollection這樣就必須null

解決你的問題,你需要初始化_keyedEntryCollection領域的實例被創建時:

protected KeyedDictionaryEntryCollection<TKey> _keyedEntryCollection 
    = new KeyedDictionaryEntryCollection<TKey>(); 

或者你也可以在適當的構造函數執行初始化來代替。

0

問題是,最可能ObservableDictionary使用KeyValuePair作爲其基礎項目類型。這個類是不可變的,所以它不能用無參數的構造函數構造,然後設置其Key和Value成員。所以,它不能用於大多數期望這種行爲的反序列化器。

+0

但我可以使用標準的BinaryFormatter對它進行序列化! 我真的不能像往常一樣反序列化它嗎? – Denis

+0

序列化不是一個問題,要序列化,您需要讀取鍵和值屬性,並將它們寫入序列化上下文中。但是,當您讀取序列化數據時,解串器將實例化一個沒有任何參數的KeyValuePair,然後嘗試將它們設置爲從數據中讀取的值。對於像KeyValuePair <,>這樣的類,這會失敗,所以值保持爲空 –