18

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?

Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114

1 Answers1

18

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Many thanks for the clarification, Marc. I suspected that this was the case (the guidance has not been updated for .NET 4.5 for example), but it feels more ensuring to get confirmation from a reliable source. – Anders Gustafsson Nov 28 '12 at 12:41
  • 1
    @Anders I've added some notes into the "Community Content" section - they might appear at some point. – Marc Gravell Nov 28 '12 at 12:43
  • 1
    Some of this is enforced at run-time, though. For example, if you try to add something to the Exception.Data collection it will throw an exception on the full Framework, but not on Silverlight. – John Gietzen Apr 20 '13 at 16:29