开发者

DataContractSerializer and List(Of Exception)

开发者 https://www.devze.com 2022-12-18 07:54 出处:网络
How can I serialize a list of Exception objects (also including derived exceptions, eg. FileNotFoundException) with the DataContractSerializer?

How can I serialize a list of Exception objects (also including derived exceptions, eg. FileNotFoundException) with the DataContractSerializer?

I always get an error about the serializer not knowing the types in the list so i devised a workaround.

It looked something like this:

Dim XmlSerializer As New DataContractSerializer( _
    ExceptionsList.GetType(), ExceptionsList.Select(Function(i) i.GetType))
XmlSerializer.WriteObject(Stream, List)

This works. I just add all the different exception types to the list of known types and it works. But on deserialization I am stuck. The problem is i dont know the types of exceptions stored in the file beforeha开发者_开发技巧nd.


I think you're SOL. The serializer needs to know the types that might be in the input.

You could try using the NetDataContractSerializer. That outputs type metadata in addition to the data being serialized.


You might try unboxing the Exceptions going into the serializer to System.Exception. I don't if this would have any real impact, but at least the type passing through might register as something standard.

(shot in the dark)

0

精彩评论

暂无评论...
验证码 换一张
取 消