在C#中,我怎么能序列化System.Exception的? (.NET CF 2.0) [英] In C#, how can I serialize System.Exception? (.Net CF 2.0)

查看:388
本文介绍了在C#中,我怎么能序列化System.Exception的? (.NET CF 2.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个例外的MS消息队列。当我尝试它,我得到一个异常。于是,我就用它仍然会引发异常XmlSerializer的简化,但它给了我一个多一点信息:

I want to write an Exception to an MS Message Queue. When I attempt it I get an exception. So I tried simplifying it by using the XmlSerializer which still raises an exception, but it gave me a bit more info:

{有反射式错误
  'System.Exception的'。}

{"There was an error reflecting type 'System.Exception'."}

使用的InnerException:

with InnerException:

{无法序列成员
  类型System.Exception.Data
  System.Collections.IDictionary,
  因为它实现IDictionary的。}

{"Cannot serialize member System.Exception.Data of type System.Collections.IDictionary, because it implements IDictionary."}

样code:

        Exception e = new Exception("Hello, world!");
        MemoryStream stream = new MemoryStream();
        XmlSerializer x = new XmlSerializer(e.GetType()); // Exception raised on this line

        x.Serialize(stream, e);
        stream.Close();

编辑:
我试图保持这个简单越好,但我可能有些过头了。我希望整个位,堆栈跟踪,信息,自定义异常类型和自定义异常属性。我甚至可能需要再次抛出异常。

I tried to keep this a simple as possible, but I may have overdone it. I want the whole bit, stack trace, message, custom exception type, and custom exception properties. I may even want to throw the exception again.

推荐答案

我想你基本上有两种选择:

I think you basically have two options:


  1. 请你自己手动序列化(可能不希望这样做)。 XML序列化将肯定无法工作,由于您在内部异常得到确切的消息。

  2. 创建自己的自定义(序列化)异常类,注入从抛出的异常数据到您的自定义的和序列化。

这篇关于在C#中,我怎么能序列化System.Exception的? (.NET CF 2.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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