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?
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