Using Exception.Data

前端 未结 4 1843
梦谈多话
梦谈多话 2020-12-13 23:33

How have you used the Exception.Data property in C# projects that you\'ve worked on?

I\'d like answers that suggest a pattern, rather than those that are very speci

4条回答
  •  我在风中等你
    2020-12-14 00:12

    I have used it when I knew the exception I was creating was going to need to be serialized. Using Reflector one day, I found that Excepion.Data gets stuck into and pulled from serialization streams.

    So, basically, if I have properties on a custom exception class that are already serializable types, I implement them on the derived class and use the underlying data object as their storage mechanism rather than creating private fields to hold the data. If properties of my custom exception object require more advanced serialization, I generally implement them using backing private fields and handle their serialization in the derived class.

    Bottom line, Exception.Data gives you serialization for free just by sticking your properties into it -- but just remember those items need to be serializable!

提交回复
热议问题