5

I have bunch of custom exceptions in my solution's legacy code. And I want to test all

the custom exceptions introduced in my projects to see if they are Serializable (XML)

So, what should my tests check to pass when a custom exception is serializable?

What are the least requirements to have to say that a custom exception is serializable?

pencilCake
  • 51,323
  • 85
  • 226
  • 363
  • Making them serializable is no problem, as long as the type name gets serialized. Deserialization would be the hard part, exceptions invariably get their property values from their constructor. Then again, deserializing an exception from an XML file doesn't make much sense. – Hans Passant Dec 09 '10 at 14:48

4 Answers4

2

You can check if all your exception classes implement the IXmlSerializable interface:

Assert.IsTrue(yourExceptionInstance is IXmlSerializable);
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
1

The Exception base class exposes a public property Data which implements IDictionary which is not supported by the default .NET XML serialization mechanism.

So I believe that in order for you to XML serialize an exception you will be forced to implement IXmlSerializable in order to provide custom XML serialization logic.

Based on that you can check that your classes implement that specific interface, like Frédéric demonstrated in his answer.

João Angelo
  • 56,552
  • 12
  • 145
  • 147
1

I would suggest using xmlSerializer.CanDeserialize(..) method.

MSDN

Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
0
typeof(MyException).IsSerializeable
Jaster
  • 8,255
  • 3
  • 34
  • 60