C# 尝试反序列化从Exception继承的类时,Json.net失败

C# 尝试反序列化从Exception继承的类时,Json.net失败,c#,json,json.net,C#,Json,Json.net,我有一个类SearchError,它继承自Exception,每当我尝试从有效的json对其进行反序列化时,我会得到以下异常: ISerializable类型“SearchError”没有有效的构造函数。要正确实现ISerializable,应该存在一个接受SerializationInfo和StreamingContext参数的构造函数。路径“”,第1行,位置81 我尝试实现建议的缺少构造函数,但没有任何帮助 这是实现建议的构造函数后的类: public class APIError : Ex

我有一个类
SearchError
,它继承自
Exception
,每当我尝试从有效的json对其进行反序列化时,我会得到以下异常:

ISerializable类型“SearchError”没有有效的构造函数。要正确实现ISerializable,应该存在一个接受SerializationInfo和StreamingContext参数的构造函数。路径“”,第1行,位置81

我尝试实现建议的缺少构造函数,但没有任何帮助

这是实现建议的构造函数后的类:

public class APIError : Exception
{
    [JsonProperty("error")]
    public string Error { get; set; }

    [JsonProperty("@http_status_code")]
    public int HttpStatusCode { get; set; }

    [JsonProperty("warnings")]
    public List<string> Warnings { get; set; }

    public APIError(string error, int httpStatusCode, List<string> warnings) : base(error)
    {
        this.Error = error;
        this.HttpStatusCode = httpStatusCode;
        this.Warnings = warnings;
    }

    public APIError(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
        : base(info, context)
    {
        Error = (string)info.GetValue("error", typeof(string));
        HttpStatusCode = (int)info.GetValue("@http_status_code", typeof(int));
        Warnings = (List<string>)info.GetValue("warnings", typeof(List<string>));
    }
}
public class SearchError : Exception
{
    public SearchError(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
    {

    }
}
公共类API错误:异常
{
[JsonProperty(“错误”)]
公共字符串错误{get;set;}
[JsonProperty(“@http\u status\u code”)]
公共int HttpStatusCode{get;set;}
[JsonProperty(“警告”)]
公共列表警告{get;set;}
公共APIError(字符串错误、int-httpStatusCode、列表警告):base(错误)
{
这个错误=错误;
this.HttpStatusCode=HttpStatusCode;
这个。警告=警告;
}
公共APIError(System.Runtime.Serialization.SerializationInfo信息,System.Runtime.Serialization.StreamingContext上下文)
:base(信息、上下文)
{
Error=(string)info.GetValue(“Error”,typeof(string));
HttpStatusCode=(int)info.GetValue(@http_status_code),typeof(int));
Warnings=(List)info.GetValue(“Warnings”,typeof(List));
}
}
现在我得到了以下异常(也在json.net代码中):

找不到成员“ClassName”


我还尝试实现与中相同的解决方案,但也遇到了上述相同的错误。

正如错误所述,您缺少序列化构造函数:

public class APIError : Exception
{
    [JsonProperty("error")]
    public string Error { get; set; }

    [JsonProperty("@http_status_code")]
    public int HttpStatusCode { get; set; }

    [JsonProperty("warnings")]
    public List<string> Warnings { get; set; }

    public APIError(string error, int httpStatusCode, List<string> warnings) : base(error)
    {
        this.Error = error;
        this.HttpStatusCode = httpStatusCode;
        this.Warnings = warnings;
    }

    public APIError(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
        : base(info, context)
    {
        Error = (string)info.GetValue("error", typeof(string));
        HttpStatusCode = (int)info.GetValue("@http_status_code", typeof(int));
        Warnings = (List<string>)info.GetValue("warnings", typeof(List<string>));
    }
}
public class SearchError : Exception
{
    public SearchError(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
    {

    }
}

这个问题在这里得到了回答:

添加新构造函数

公共错误(SerializationInfo,StreamingContext上下文){}
解决了我的问题

这里是完整的代码:

[可序列化]
公共类错误:异常
{
公共字符串错误消息{get;set;}
公共错误(SerializationInfo、StreamingContext上下文){
如果(信息!=null)
this.ErrorMessage=info.GetString(“ErrorMessage”);
}
公共覆盖无效GetObjectData(SerializationInfo、StreamingContext上下文)
{
base.GetObjectData(信息、上下文);
如果(信息!=null)
info.AddValue(“ErrorMessage”,this.ErrorMessage);
}
}

与他人共享您的代码..这个问题已经在这里得到了回答:我尝试了您的建议,之前我已经尝试过,现在我遇到了一些其他异常(见上文)。“:base(info,context)”抛出异常“未找到成员“ClassName”。@joelnet:这是您的代码的问题,而不是答案。请参见MSDN链接,例如:@joelnet:答案中的示例有效。我们有几十个这样配置的类。此外,MSDN链接还包含一个工作示例。如果您的代码存在特定问题,那么您应该创建一个新问题,而不是对答案进行否决,然后在注释中寻求帮助。序列化构造函数并不总是丢失,它可以得到保护,例如在实体框架中使用DbUpdateConcurrencyException。而且,这种回答也不好,因为如果你没有这个类的所有权,它就不能被应用。这看起来更像是JSON.NET中的一个bug,因为它无法找到受保护的构造函数。这个答案是没有用的,因为你不能总是向你不拥有的类添加一个成员。例如,我在尝试序列化实体框架引发的DbUpdateConcurrencyException时遇到此错误。我无法控制该类或其成员,也无法控制抛出什么样的实例。@Lukasz Lysik正确地认为答案需要改进,但事实上,最好的改进是简化。通过单步执行代码并保存整个对象及其属性,我已经确认必须在GetObjectData中的唯一语句是第一个语句base.GetObjectData(info,context);我已经确认每个属性都会被填充,包括在.NET2.0中受保护的HRESULT。构造函数也是如此。有什么特别的原因吗?这是JSON.NET中的错误吗?添加到数百个自定义异常中是一件可怕的事情。