开发者

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

开发者 https://www.devze.com 2023-01-30 06:05 出处:网络
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)

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 custo开发者_如何学JAVAm exception is serializable?

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


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

Assert.IsTrue(yourExceptionInstance is IXmlSerializable);


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.


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

MSDN


typeof(MyException).IsSerializeable
0

精彩评论

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