gpt4 book ai didi

c# - 将异常序列化为可抛出

转载 作者:太空狗 更新时间:2023-10-29 19:46:18 25 4
gpt4 key购买 nike

虽然我意识到有一个类似的问题 ( How to serialize an Exception object in C#? ),尽管该页面上的答案很有帮助,但它们并没有完全解决问题或回答所提出的问题。

我认为问题是如何序列化对象以允许将其重构(反序列化)到同一个对象中。我尝试使用 davogones 给出的解决方案和 Antony Booth , 但如果不在消费端添加 System.Exception 基类(如:SerializationException: Exception),就不可能(通过它们自己)实际使用这些类型可以抛出的异常对象。

在我继续之前,让我解释一下最后一句话。我试过使用 Antony Booth's solution在 Web 服务中(该服务包含可序列化对象的定义)试图让所有消费者使用相同的异常(希望创建可重用的可序列化异常类型而不是重新创建它)。

不幸的是,由于这两种类型都不是显式派生自 System.Exception,因此您不能throw 它们,这显然很有用。就像我上面提到的,似乎在消费端的类型类定义中添加 : Exception 允许抛出对象,但这需要编辑自动生成的 WSDL/web 服务代码,这似乎直觉上对我来说就像是一种不好的/不可维护的做法(如果我错了请纠正我)。

我的第一个问题是,是否可以序列化 System.Exception 或创建可以序列化的派生类型,如果可能的话,人们将如何去做?我应该提一下,我已经看过似乎是 reconstitute the Exception object 的官方方式。 ,但恐怕我不太了解。

我的第二个问题是关于 System.Exception 本身的架构。我想知道的是为什么 System.Exception 类型被标记为 [Serializable] 当它被记录并且显然旨在禁止您正确序列化它时(在至少对于 XML) 因为它是 Data 对象实现 IDictionary?

来自 MSDN:

Q: Why can't I serialize hashtables?

A: The XmlSerializer cannot process classes implementing the IDictionary interface. This was partly due to schedule constraints and partly due to the fact that a hashtable does not have a counterpart in the XSD type system. The only solution is to implement a custom hashtable that does not implement the IDictionary interface.

鉴于 XML 正在成为(如果不是已经成为)数据传输的新标准(尽管如此,Microsoft 正式推荐),不允许 .NET 中唯一可以抛出的对象类型似乎是荒谬的愚蠢XML 序列化。

我期待听到所有 SO 的一些想法(特别是因为这是我的第一篇文章)。

如果您有任何疑问或需要澄清,请随时告诉我。


注意:我刚刚找到this SO post ,这似乎回答了几个问题,但我想我想自己解决一下。不过,如果它太接近副本,请告诉我。

最佳答案

您可以创建一个派生自Exception 的类,并通过实现ISerializable 自行进行序列化和反序列化。界面。

示例取自 wrox forums , 子类化 ApplicationException:

编辑:如前所述,ApplicationException 已弃用。使用基础 Exception 类应该可以正常工作。

using System;
using System.Collections;
using System.Runtime.Serialization;

namespace Common.CustomExceptions
{

/// <summary>
/// Custom exception.
/// </summary>
[Serializable]
public class CustomExceptionBase: ApplicationException
{

// Local private members
protected DateTime _dateTime = DateTime.Now;
protected String _machineName = Environment.MachineName;
protected String _exceptionType = "";
private String _exceptionDescription = "";
protected String _stackTrace = "";
protected String _assemblyName = "";
protected String _messageName = "";
protected String _messageId = "";
protected Hashtable _data = null;
protected String _source = "";
protected Int32 _exceptionNumber = 0;

public CustomExceptionBase(): base()
{
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}

public CustomExceptionBase(Int32 exceptionNumber): base()
{
this._exceptionNumber = exceptionNumber;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}

public CustomExceptionBase(Int32 exceptionNumber, String message): base(message)
{
this._exceptionNumber = exceptionNumber;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}

public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException):
base(message, innerException)
{
this._exceptionNumber = exceptionNumber;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}

public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException, String messageName, String mqMessageId):
base(message, innerException)
{
this._exceptionNumber = exceptionNumber;
this._messageId = mqMessageId;
this._messageName = messageName;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}

public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException, String messageName, String mqMessageId, String source):
base(message, innerException)
{
this._exceptionNumber = exceptionNumber;
this._messageId = mqMessageId;
this._messageName = messageName;
this._source = source.Equals("") ? this._source : source;
if (Environment.StackTrace != null)
this._stackTrace = Environment.StackTrace;
}


#region ISerializable members

/// <summary>
/// This CTor allows exceptions to be marhalled accross remoting boundaries
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
protected CustomExceptionBase(SerializationInfo info, StreamingContext context) :
base(info,context)
{
this._dateTime = info.GetDateTime("_dateTime");
this._machineName = info.GetString("_machineName");
this._stackTrace = info.GetString("_stackTrace");
this._exceptionType = info.GetString("_exceptionType");
this._assemblyName = info.GetString("_assemblyName");
this._messageName = info.GetString("_messageName");
this._messageId = info.GetString("_messageId");
this._exceptionDescription = info.GetString("_exceptionDescription");
this._data = (Hashtable)info.GetValue("_data", Type.GetType("System.Collections.Hashtable"));
}

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("_dateTime", this._dateTime);
info.AddValue("_machineName", this._machineName);
info.AddValue("_stackTrace", this._stackTrace);
info.AddValue("_exceptionType", this._exceptionType);
info.AddValue("_assemblyName", this._assemblyName);
info.AddValue("_messageName", this._messageName);
info.AddValue("_messageId", this._messageId);
info.AddValue("_exceptionDescription", this._exceptionDescription);
info.AddValue("_data", this._data, Type.GetType("System.Collections.Hashtable"));
base.GetObjectData (info, context);
}

#endregion
}
}

关于c# - 将异常序列化为可抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3609384/

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