How to deserialize object derived from Exception class using Json.net?

前端 未结 3 1799
时光取名叫无心
时光取名叫无心 2020-12-03 21:10

I\'m trying to deserialize object derived from Exception class:

[Serializable]
public class Error : Exception, ISerializable
{
    public string ErrorMessage         


        
3条回答
  •  悲哀的现实
    2020-12-03 21:50

    Adding a new constructor

    public Error(SerializationInfo info, StreamingContext context){}
    solved my problem.

    Here complete code:

    [Serializable]
    public class Error : Exception
    {
        public string ErrorMessage { get; set; }
    
        public Error(SerializationInfo info, StreamingContext context) 
        {
            if (info != null)
                this.ErrorMessage = info.GetString("ErrorMessage");
        }
    
        public override void GetObjectData(SerializationInfo info,StreamingContext context)
        {
            base.GetObjectData(info, context);
    
            if (info != null)
                info.AddValue("ErrorMessage", this.ErrorMessage);
        }
    }
    

提交回复
热议问题