C# Exception的子类Serializable警告

编译时发出的警告:
警告 CA2237[1] 将 [Serializable] 添加到 'HardwareException',原因是此类型实现了 ISerializable。

Cause[1]


An externally visible type implements the System.Runtime.Serialization.ISerializable interface and the type is not marked with the System.SerializableAttribute attribute. The rule ignores derived types whose base type is not serializable.

Rule Description 


To be recognized by the common language runtime as serializable, types must be marked with the SerializableAttribute attribute even if the type uses a custom serialization routine through implementation of the ISerializable interface.

 

To fix a violation of this rule, apply the SerializableAttribute attribute to the type.

应对:

  如果不跨越AppDomain传递/抛出异常,什么也不不用做。——这是静态分析器(FxCop tool[2])的过爱之心。[3]

reference:

[1] Mark ISerializable types, https://msdn.microsoft.com/library/ms182350.aspx

[2] FxCop, https://msdn.microsoft.com/en-us/library/bb429476(v=vs.80).aspx

[3] Stack Overflow, http://stackoverflow.com/questions/11327404/deriving-from-exception-classes-warning-ca2237-mark-iserializable-types-with-s

原文地址:https://www.cnblogs.com/jjseen/p/5138621.html