为什么我不能使用 DataContractSerializer 序列化对象?
我正在尝试使用 DataContractSerializer 序列化类型,但出现以下异常。 这不适用于 SOA 服务,但如果可能的话,我仍然想使用 DataContractSerializer。 我正在使用.Net 3.5 SP1。
类型 'System.DelegateSerializationHolder+DelegateEntry' 带有数据合约名称 'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System' 预计不会。 添加任何类型不 静态已知已知列表 类型 - 例如,通过使用 KnownTypeAttribute 属性或通过 将它们添加到已知类型列表中 传递给 DataContractSerializer。
I'm trying to serialize a type using the DataContractSerializer and am getting the exception below. This isn't for an SOA service, but I would still like to use the DataContractSerializer if possible. I am using .Net 3.5 SP1.
Type
'System.DelegateSerializationHolder+DelegateEntry'
with data contract name
'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System'
is not expected. Add any types not
known statically to the list of known
types - for example, by using the
KnownTypeAttribute attribute or by
adding them to the list of known types
passed to DataContractSerializer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你能发布你的类定义吗?
看起来您正在尝试序列化一个具有委托类型字段的类,我很确定这会让序列化器卡住。
您是否使用 DataContract / DataMember 属性来装饰您的类? 在 3.5 SP1 中,序列化程序有一个默认行为,如果类中未标记这些属性,则默认序列化类中的所有公共内容。 也许您应该使用 DataMember 属性明确标记需要序列化的每个属性,并忽略那些不应该序列化的属性。
除此之外,我们需要查看您的类定义以获得更多帮助。
Can you post your class definition?
It seems like you are trying to serialize a class which has a field of type delegate, which I'm pretty sure will make the serializer choke on.
Did you decorate your class with the DataContract / DataMember attributes? In 3.5 SP1 there is a default behavior for the serializer that serializes everything public in a class by default if it is not marked with those attributes. Maybe you should explicitely mark each property that needs to be serialized with a DataMember attribute and leave out those that should not be.
Other than that, we would need to see your class definition for more help.
有 3 种可能的方法可以避免 http://blogs.microsoft.co.il/blogs/oshvartz/archive/2009/10/10/passing-event-handlers-over-wcf.aspx
There are 3 possible approaches to avoid the error described in http://blogs.microsoft.co.il/blogs/oshvartz/archive/2009/10/10/passing-event-handlers-over-wcf.aspx