gpt4 book ai didi

c# - 在 C# 中,如何序列化 System.Exception? (.Net CF 2.0)

转载 作者:IT王子 更新时间:2023-10-29 04:49:00 25 4
gpt4 key购买 nike

我想向 MS 消息队列写入异常。当我尝试它时,我得到一个异常(exception)。所以我尝试使用 XmlSerializer 来简化它,它仍然会引发异常,但它给了我更多信息:

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

内部异常:

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

示例代码:

        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();

编辑:我尽量保持简单,但我可能做得过头了。我想要整个位、堆栈跟踪、消息、自定义异常类型和自定义异常属性。我什至可能想再次抛出异常。

最佳答案

我正在查看 Jason Jackson 的回答,但即使 System.Exception 实现了 ISerializable,我还是遇到了问题,这对我来说没有意义。因此,我通过将异常包装在一个使用 BinaryFormatter 的类中来绕过 XmlSerializer。当 MS 消息队列对象的 XmlSerialization 启动时,它将看到的是一个带有公共(public)字节数组的类。

这是我想出的:

public class WrappedException {
public byte[] Data;

public WrappedException() {
}

public WrappedException(Exception e) {
SetException(e);
}

public Exception GetException() {
Exception result;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream(Data);
result = (Exception)bf.Deserialize(stream);
stream.Close();
return result;
}

public void SetException(Exception e) {
MemoryStream stream = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, e);
Data = stream.ToArray();
stream.Close();
}
}

第一个测试完美运行,但我仍然担心自定义异常。所以我把自己的自定义异常放在一起。然后我只是在空白表格上放了一个按钮。这是代码:

[Serializable]

public class MyException : Exception, ISerializable {
public int ErrorCode = 10;
public MyException(SerializationInfo info, StreamingContext context)
: base(info, context) {

ErrorCode = info.GetInt32("ErrorCode");
}

public MyException(string message)
: base(message) {
}

#region ISerializable Members
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context) {

base.GetObjectData(info, context);
info.AddValue("ErrorCode", ErrorCode);
}

#endregion
}

private void button1_Click(object sender, EventArgs e) {
MyException ex = new MyException("Hello, world!");
ex.ErrorCode = 20;
WrappedException reply = new WrappedException(ex);
XmlSerializer x = new XmlSerializer(reply.GetType());
MemoryStream stream = new MemoryStream();
x.Serialize(stream, reply);
stream.Position = 0;
WrappedException reply2 = (WrappedException)x.Deserialize(stream);
MyException ex2 = (MyException)reply2.GetException();
stream.Close();
Text = ex2.ErrorCode.ToString(); // form shows 20

// throw ex2;

}

尽管我查找的所有其他异常类型似乎都标有 SerializableAttribute,但我将不得不小心未标有 SerializableAttribute 的自定义异常。

编辑:超越 self 。我没有意识到 BinaryFormatter 没有在 CF 上实现。

编辑:以上代码片段在桌面项目中。在 CF 版本中,WrappedException 基本上看起来与我只是需要实现我自己的 BinaryFormater,但我非常愿意接受有关这方面的建议。

关于c# - 在 C# 中,如何序列化 System.Exception? (.Net CF 2.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/390051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com