1
我有一個ContactForm
對象,它包含幾個嵌套的集合對象。當我嘗試序列化對象時,代碼卡在SectionCollectionObject
內的循環中。C#如何在收集序列化期間停止循環代碼導致stackoverflow?
這裏是執行serialize()
調用的代碼:
public static ContactForm SaveForm(ContactForm cf)
{
if (cf != null)
{
XmlSerializer xs = new XmlSerializer(cf.GetType());
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
xs.Serialize(sw, cf);
}
}
// ...
}
節目被在「得到」語句停留在循環,直到它拋出一個StackOverflowException
。需要修改或添加哪些代碼才能超越這一點?
這裏是SectionObjectCollection
類:
[Serializable, XmlInclude(typeof(Section))]
public sealed class SectionObjectCollection : Collection<Section>
{
public Section this[int index]
{
get {
return (Section)this[index]; //loops here with index always [0]
}
set {
this[index] = value;
}
}
}
這裏是Section
類集合類繼承自:
public class Section
{
public Section()
{
Questions = new QuestionObjectCollection();
}
public int SectionDefinitionIdentity {get;set;}
public string Name {get;set;}
public string Description {get;set;}
public bool ShowInReview {get;set;}
public int SortOrder {get;set;}
public QuestionObjectCollection Questions
{
get;
private set;
}
}
您可以發佈您收到異常的堆棧跟蹤嗎? –