简体   繁体   English

自动生成类的反序列化

[英]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.

相关问题 继承自Entity Framework中的自动生成的类 - Inheriting from auto-generated classes in Entity Framework 自动生成的代码 - auto-generated Code 自动生成的备用密钥 - Auto-generated Alternate Key NullReferenceException与自动生成的表 - NullReferenceException with Auto-Generated Tables 记录自动生成的代码 - Documenting auto-generated code Reference.cs中的Service Reference自动生成的类未作为内部生成 - Service Reference auto-generated classes in Reference.cs not being generated as Internal Visual Studio服务参考和自动生成的代理类-为什么不应该在您的类/项目中传递它们? - Visual Studio Service Reference, and Auto-Generated Proxy Classes - Why should they not be passed around your classes/projects? 在.NET中使用SOAP WS时使用自动生成的类/对象? - Use auto-generated classes/objects when using a SOAP WS in .NET? 将类添加到item.GetPropertyValue()自动生成的p元素(Umbraco) - Adding classes to item.GetPropertyValue() auto-generated p elements (Umbraco) 将类从一个 C# 项目导入另一个项目时,是否可以排除自动生成的文件? - Is it possible to exclude auto-generated files when importing classes from one C# project to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM