我使用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);
在這種情況下你有什麼建議嗎?
正如我所看到的問題是在'AddEntry'中,而不是在OnDeserialization。郵政編碼的AddEntry。 – Tony
這是AddEntry的代碼。我編輯後 – Denis
異常堆棧跟蹤將錯誤指向「ObservableDictionary.cs」行196.當您得到異常時,在該行代碼中使用的引用爲「null」。 –