R – DataContractSerializer and List(Of Exception)


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 beforehand.

Best Solution

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.