在 C# 中序列化和反序列化自定义异常

发布于 2025-01-09 02:40:26 字数 1442 浏览 0 评论 0原文

我目前正在处理异常,并且创建了一个实现父 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昵称有卵用 2025-01-16 02:40:26

如果正在序列化的对象图中的任何类型未应用 SerializedAttribute 属性,则公共语言运行时将引发 SerializationException。

此处:https://learn。 microsoft.com/en-us/dotnet/api/system.serializedattribute?view=net-6.0#remarks

尝试删除 SerializedAttribute

The common language runtime throws SerializationException if any type in the graph of objects being serialized does not have the SerializableAttribute attribute applied.

Here: https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute?view=net-6.0#remarks

Try removing the SerializableAttribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文