public 属性 的 setter 将 "value" 分配给集合时出现序列化错误

Serialization error when setter of public property assigns "value" to a collection

我知道这是导致错误的原因,但我不明白为什么以及如何解决这个问题。我正在寻找一种方法来避免创建数百个变量

将其替换为

initialStopCollection[n].BufferUAbsolute

Serialization Error

您的代码应更改为:

set
{
    int itemIndex = 1;
    if (initialStopCollection == null) initialStopCollection = new ...; // your initialStopCollection is null. Create new one
    if (itemIndex >= initialStopCollection.Count) // Two few items, create new
    {
        for (int i = initialStopCollection.Count; i <= itemIndex; i++)
        {
            initialStopCollection.Add(new ...);
        }
    }
    if (initialStopCollection[itemIndex] == null) initialStopCollection[itemIndex] = new ... ; // This item is not initialized, create new
    initialStopCollection[itemIndex].BufferUAbsolute = value;
}