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

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

问题描述

是否所有的.NET异常对象序列化?

Can all .NET exception objects be serialized?

推荐答案

是的,没有。杰夫和其他人指出的那样,所有的异常类应该而且几乎总是序列化。如果你穿过一个特定的异常类,是不是序列化运行,它很可能是一个错误。

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.

数据属性的默认实现并做一些基本检查,以确保被添加的对象是可序列化。但它是有缺陷的几种方法

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.IsSerialable(不是100%的固体检查)完成。
  • 在该物业是虚拟的。派生的异常类型可以重写,并使用一个Hashtable这的确0检查。

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

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