4

Can anyone give me an example or point me to a resource on how to use Protobuf-Net to serialize/deserialize some of the bulit-in system classes?

Specifically I'm just trying to serialize/deserialize the base Exception class and all other exception classes that inherit from it. Will I have to create a new RunTypeModel that specifies every possible exception class that I will ever need to serialize, or can I somehow tell Protobuf-Net to serialize them all the same way without listing every single one?

Any help is very appreciated since I am brand new to Protobuf-Net and I'm still trying to understand it all.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
WiredWiz
  • 646
  • 8
  • 18

2 Answers2

4

protobuf-net is designed to serialize DTO models, but not exceptions - very similar to XmlSerializer etc (but binary rather than xml, obviously). Serialising exceptions is not currently built in. It may be possible to hack some things, but this isn't really a designed feature.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I ultimately solved this problem by creating an ExceptionProxy class that uses a little ugly reflection to extract the data from an exception and serialize it. I then re-hydrate the exception graph on the other side using the deserialized ExceptionProxy class information and more reflection. It isn't pretty, but it works quite well. – WiredWiz Feb 12 '16 at 14:56
0

You really can´t serialize a class like

public class MyTest
{
    [ProtoMember(1)]
    public Exception MyException { get; set; }
}

But doing a small change will be possible to serialize

public class MyTest
{
    [ProtoMember(1, DynamicType = true)]
    public Object MyException { get; set; }
}

This was the only way that I found to serialize an exception.

  • Tried, this didn't seem to work for me. This worked: http://stackoverflow.com/questions/94488/what-is-the-correct-way-to-make-a-custom-net-exception-serializable – Contango Feb 07 '16 at 17:26