在 C# 中序列化和反序列化自定义异常
我目前正在处理异常,并且创建了一个实现父 Exception 类的自定义异常。
当我尝试序列化或反序列化此类自定义异常时,就会出现问题。
错误如下:“System.Runtime.Serialization.SerializationException:未找到'成员'InnerException'。'”
我参考了互联网上的许多来源,但无法解决该问题。我将代码附在这个正文中。
[Serializable]
public class CustomException : Exception
{
public const string DefaultExceptionDescription = "temp";
public string ExceptionDescription { get; set; }
public string ReferenceId { get; private set; } = "temp";
public CustomException(string ExceptionDescription = DefaultExceptionDescription)
{
ExceptionDescription = ExceptionDescription;
}
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected CustomException(SerializationInfo info, StreamingContext context) : base(info, context)
{
if (info != null)
{
ExceptionDescription = info.GetString(nameof(ExceptionDescription));
ReferenceId = info.GetString(nameof(ReferenceId));
}
}
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info != null)
{
info.AddValue(nameof(ExceptionDescription), ExceptionDescription);
info.AddValue(nameof(ReferenceId), ReferenceId);
}
base.GetObjectData(info, context);
}
}
提前致谢!!!
I'm currently working on exceptions and I've created a custom exception which implements the parent Exception class.
The problem arises when I try to Serialize or Deserialize such a custom exception.
The error is as follows : "System.Runtime.Serialization.SerializationException: 'Member 'InnerException' was not found.'"
I've referred to many sources on the internet, but was not able to fix the issue. I'm attaching the code with this body.
[Serializable]
public class CustomException : Exception
{
public const string DefaultExceptionDescription = "temp";
public string ExceptionDescription { get; set; }
public string ReferenceId { get; private set; } = "temp";
public CustomException(string ExceptionDescription = DefaultExceptionDescription)
{
ExceptionDescription = ExceptionDescription;
}
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected CustomException(SerializationInfo info, StreamingContext context) : base(info, context)
{
if (info != null)
{
ExceptionDescription = info.GetString(nameof(ExceptionDescription));
ReferenceId = info.GetString(nameof(ReferenceId));
}
}
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info != null)
{
info.AddValue(nameof(ExceptionDescription), ExceptionDescription);
info.AddValue(nameof(ReferenceId), ReferenceId);
}
base.GetObjectData(info, context);
}
}
Thanks in Advance!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此处:https://learn。 microsoft.com/en-us/dotnet/api/system.serializedattribute?view=net-6.0#remarks
尝试删除
SerializedAttribute
Here: https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute?view=net-6.0#remarks
Try removing the
SerializableAttribute