[英]serialize HybridDictionary to byte[]
我寫了下面的代碼
HybridDictionary state = new HybridDictionary();
using (MemoryStream buffer = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(buffer , state);
_backup.Push(buffer .ToArray());
}
但我在formatter.serialize(st,state)上遇到錯誤,如下所示:
“未將用戶代碼Message =“ Type'System.ComponentModel.ReflectPropertyDescriptor'在程序集'System,版本= 2.0.0.0,區域性=中性,PublicKeyToken = b77a5c561934e089'中未處理System.Runtime.Serialization.SerializationException標記為可序列化。”
這是什么意思 ?
加
[field:NonSerializedAttribute()]
public event MyEventHandler SomeEvent;
參加您的活動。 這將忽略它們的序列化。
您應該避免在事件上序列化已注冊的事件處理程序(正如您在上一個答案的注釋中所述,您的對象確實具有事件)。 如果其中一個已注冊的處理程序無法序列化,則該對象將拋出異常。
這個想法源於有關Sanity Free dot org的論壇帖子 。
這是我的實現方式:
/// <summary>
/// Occurs when a property value changes.
/// </summary>
/// <devdoc>Do not serialize this delegate, since we do not want to have
/// the event listener persisted.</devdoc>
[NonSerialized]
private PropertyChangedEventHandler _propertyChanged;
/// <summary>
/// Occurs when a property value changes.
/// </summary>
/// <remarks>Effectively, this is fired whenever the wrapped MapXtreme theme
/// of the AssociatedTheme property gets changed
/// via the MapXtreme API.</remarks>
/// <devdoc>The implementation using the public event and the private delegate is
/// to avoid serializing the (probably non-serializable) listeners to this event.
/// see http://www.sanity-free.org/113/csharp_binary_serialization_oddities.html
/// for more information.
/// </devdoc>
public event PropertyChangedEventHandler PropertyChanged
{
[MethodImpl(MethodImplOptions.Synchronized)]
add
{
_propertyChanged = (PropertyChangedEventHandler)Delegate.Combine(_propertyChanged, value);
}
[MethodImpl(MethodImplOptions.Synchronized)]
remove
{
_propertyChanged = (PropertyChangedEventHandler)Delegate.Remove(_propertyChanged, value);
}
}
最好的問候,馬塞爾
就像錯誤所述,您的詞典中有System.ComponentModel.ReflectPropertyDescriptor,並且未將其標記為可序列化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.