如何在C#中序列化异常对象?(How to serialize an Exception object in C#?)

编程入门 行业动态 更新时间:2024-10-11 11:21:06
如何在C#中序列化异常对象?(How to serialize an Exception object in C#?)

我试图在C#中序列化一个Exception对象。 然而,似乎这是不可能的,因为Exception类没有被标记为[Serializable] 。 有办法解决这个问题吗?

如果应用程序执行过程中出现问题,我想通知发生的异常。

我的第一反射是序列化它。

I am trying to serialize an Exception object in C#. However, it appears that it is impossible since the Exception class is not marked as [Serializable]. Is there a way to work around that?

If something goes wrong during the execution of the application, I want to be informed with the exception that occurred.

My first reflex is to serialize it.

最满意答案

我之前做的是创建一个自定义的Error类。 这封装了有关异常的所有相关信息,并且是可序列化的XML。

[Serializable] public class Error { public DateTime TimeStamp { get; set; } public string Message { get; set; } public string StackTrace { get; set; } public Error() { this.TimeStamp = DateTime.Now; } public Error(string Message) : this() { this.Message = Message; } public Error(System.Exception ex) : this(ex.Message) { this.StackTrace = ex.StackTrace; } public override string ToString() { return this.Message + this.StackTrace; } }

What I've done before is create a custom Error class. This encapsulates all the relevant information about an Exception and is XML serializable.

[Serializable] public class Error { public DateTime TimeStamp { get; set; } public string Message { get; set; } public string StackTrace { get; set; } public Error() { this.TimeStamp = DateTime.Now; } public Error(string Message) : this() { this.Message = Message; } public Error(System.Exception ex) : this(ex.Message) { this.StackTrace = ex.StackTrace; } public override string ToString() { return this.Message + this.StackTrace; } }

更多推荐

本文发布于:2023-04-29 07:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335629.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   对象   序列化   如何在   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!