![](/img/trans.png)
[英]When a class is inherited from List<>, XmlSerializer doesn't serialize other attributes
[英]BinaryFormatter doesn't serialize public fields when a class is inherited from Exception, why?
誰能向我解釋為什么從Exception繼承的以下類不序列化私有字段?
從異常派生時,我應該手動序列化對象嗎?
[Serializable]
public class Test : Exception
{
private int _errNo = 0;
private string _msg = String.Empty;
public int ErrNo
{
get { return _errNo; }
}
public string Msg
{
get { return _msg; }
}
public Test(int err, string msg)
{
_errNo = err;
_msg = msg;
}
protected Test(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext kontekst)
: base(info, kontekst)
{
}
}
Exception
類實現ISerializable
,它允許Exception
重寫其序列化方式。 由於Exception
的GetObjectData
實現不使用反射來確定要序列化的字段,因此您需要覆蓋它並自己明確列出任何新字段。 然后,您還需要在構造函數中還原這些字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.