我在下面写了代码
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)上遇到错误,如下所示:
” 用户代码未处理System.Runtime.Serialization.SerializationException 消息=“在程序集'系统中键入'System.ComponentModel.ReflectPropertyDescriptor',版本= 2.0.0.0,文化=中立,PublicKeyToken = b77a5c561934e089'未标记为可序列化。”
这是什么意思?答案 0 :(得分:2)
添加
[field:NonSerializedAttribute()]
public event MyEventHandler SomeEvent;
你的活动。这将省略序列化。
答案 1 :(得分:1)
您应该避免在事件中序列化已注册的事件处理程序(正如您在上一个答案的注释中所述,您的对象确实有事件)。如果其中一个注册的处理程序不可序列化,那么这将抛出异常。
这个想法来自Forum post on 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);
}
}
最诚挚的问候,Marcel
答案 2 :(得分:0)
正如错误所示,您的字典中有System.ComponentModel.ReflectPropertyDescriptor,并且未标记为可序列化。