所有.NET异常都是可序列化的吗? [英] Are all .NET exceptions serializable?

查看:110
本文介绍了所有.NET异常都是可序列化的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将所有.NET异常对象序列化吗?

Can all .NET exception objects be serialized?

推荐答案

是和否。正如Jeff等人所指出的那样,所有的异常类应该都是可以串行化的。如果您遇到不可序列化的特定异常类,那么这很可能是一个错误。

Yes and no. As Jeff and others have pointed out, the all exception classes should and almost always are serializable. If you run across a particular exception class that is not serializable, it is quite possibly a bug.

但是当考虑可序列化时,您需要考虑立即类和属于此类型的所有类型(这是递归过程)。基础异常类有一个数据属性,它是类型IDictionary。这是有问题的,因为它允许您或任何其他人将任何对象添加到异常中。如果此对象不可序列化,则异常的序列化将失败。

But when considering serializability you need to consider both the immediate class and all types which are members of this type (this is a recursive process). The base Exception class has a Data property which is of type IDictionary. This is problematic because it allows you, or anyone else, to add any object into the Exception. If this object is not serializable then serialization of the Exception will fail.

Data属性的默认实现确实做了一些基本的检查,以确保要添加的对象是可序列化的。但是它有几种方式存在缺陷

The default implementation of the Data property does do some basic checking to ensure an object being added is serializable. But it is flawed in several ways


  • 只能对类型的可序列性进行顶级检查,而这种检查只能通过检查Type.IsSerializable (不是100%固体检查)。 

  • 属性是虚拟的。派生异常类型可以覆盖并使用执行0检查的Hashtable。

这篇关于所有.NET异常都是可序列化的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆