[英]Deserialization of Auto-Generated Classes
I'm using binary serialization to save objects to a configuration file and read them back again. 我正在使用二进制序列化将对象保存到配置文件,然后再次读取它们。 One of these classes has an event handler which I forgot to mark with the
NonSerializedAttribute
in previous builds, so this bit of information has already ended up in a few config files. 其中一个类具有事件处理程序,在以前的版本中我忘了用
NonSerializedAttribute
进行标记,因此,这些信息已经在一些配置文件中结束了。 To complicate matters, this handler is initialized with a lamda expression, which forces the compiler to automatically generate an anonymous type for it. 使事情变得复杂的是,此处理程序使用lamda表达式初始化,该表达式强制编译器为其自动生成匿名类型。
public event PropertyChangedEventHandler PropertyChanged = (sender, args) => { };
The issue is that this type name changed in subsequent builds, so when the BinaryFormatter
encounters an unknown type, it throws a SerializationException
("Cannot find member: <.ctor>b__1"). 问题在于此类型名称在后续版本中已更改,因此,当
BinaryFormatter
遇到未知类型时,它将引发SerializationException
(“找不到成员:<.ctor> b__1”)。 I can't seem to catch this exception at the right location since it occurs deep inside mscorlib.dll
, and I've had no luck with the SerializationBinder
. 我似乎无法在正确的位置捕获此异常,因为该异常发生在
mscorlib.dll
深处,并且我对SerializationBinder
没有任何运气。
Is there any way I can force the BinaryFormatter
to ignore this field, or remove it from the serialized stream entirely? 有什么方法可以强制
BinaryFormatter
忽略此字段,或者将其完全从序列化流中删除? I should add that this class in question does not implement the ISerializable interface. 我应该补充一点,该类没有实现ISerializable接口。
也许我缺少了一些东西,但是您不能只用NonSerializedAttribute
标记该字段吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.