我使用Dr.WPF的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 :(得分:1)
根据您的评论,这是抛出异常的行:
_keyedEntryCollection.Add(new DictionaryEntry(key, value));
该行中取消引用的唯一引用是_keyedEntryCollection
,因此必须为null
。
要解决您的问题,您需要在构造实例时初始化_keyedEntryCollection
字段:
protected KeyedDictionaryEntryCollection<TKey> _keyedEntryCollection
= new KeyedDictionaryEntryCollection<TKey>();
或者您可以在适当的构造函数中执行初始化。
答案 1 :(得分:0)
问题是ObservableDictionary最有可能使用KeyValuePair作为其基础项类型。此类是不可变的,因此无法使用无参数构造函数构造它,然后设置其Key和Value成员。因此,它不能与大多数期望这种行为的反序列化器一起使用。