Implementing custom exceptions in a Portable Class Library

有些话、适合烂在心里 提交于 2019-12-04 16:05:26

问题


When designing custom exceptions for .NET, MSDN provides these guidelines. In particular, the guidelines state that a custom exception:

  • should be serializable, i.e. implement ISerializable and be decorated with the [Serializable] attribute, and
  • should implement the (de)serialization constructor, i.e. protected CustomException(SerializationInfo info, StreamingContext context).

However, in a Portable Class Library neither of SerializableAttribute, ISerializable and SerializationInfo are supported.

How should I sufficiently design a custom exception in a Portable Class Library that simultaneously targets .NET Framework and one or more platforms?


回答1:


Basically, ignore that guidance - that is for full .NET, and does not apply to portable class library projects. Indeed, if we look at (say) Silverlight (which includes WP7) we see:

[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public class Exception

Frankly, the main consumer of that requirement was remoting... and that is not in huge demand now.



来源:https://stackoverflow.com/questions/13604713/implementing-custom-exceptions-in-a-portable-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!