我正在尝试序列化异常对象并将其存储到文件中,然后通过反序列化将其取回。
File.Delete(_FilePath);
using (Stream stream = File.Open(_FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
new BinaryFormatter().Serialize(stream, originalException);
}
using (Stream stream = File.Open(_FilePath, FileMode.Open, FileAccess.Read))
{
Exception deserializedException = (Exception)(new BinaryFormatter().Deserialize(stream));
// deserializedException.TargetSite is null!!!
}
但是,反序列化的 Exception 对象的 TargetSite 属性为 null。也就是说,它没有被序列化,因此它也没有被反序列化。 TargetSite 属性属于 MethodBase 类型。 (序列化 MethodBase 在 IMO 上没有意义)
有没有其他方法可以在不丢失任何数据的情况下存储和检索完整的异常对象?
最佳答案
Exception
工具 Iserializable
和 controls how it get serialized .
您可以从异常继承并覆盖 GetObjectData
。不要忘记将 Serializable
属性添加到派生类。
关于c# - 如何在不丢失任何数据的情况下序列化和反序列化异常对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40999625/