简体   繁体   English

从IList继承时无法反序列化 <T> 或IEnumerable <T>

[英]Unable to deserialize when inheriting from IList<T> or IEnumerable<T>

I've got the following class which is used to deserialize an XML feed.... 我有以下用于反序列化XML feed的类。

[Serializable]
[XmlRoot("forecast")]
public class ForecastCollection : IList<Weather>
{
    private List<Weather> _List = new List<Weather>();

    #region Implementation of IList<T>

    public IEnumerator<Weather> GetEnumerator()
    {
        return _List.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void Add(Weather item)
    {
        _List.Add(item);
    }

    public void Clear()
    {
        _List.Clear();
    }

    public bool Contains(Weather item)
    {
        return _List.Contains(item);
    }

    public void CopyTo(Weather[] array, int arrayIndex)
    {
        _List.CopyTo(array, arrayIndex);
    }

    public bool Remove(Weather item)
    {
        return _List.Remove(item);
    }

    [XmlIgnore]
    public int Count
    {
        get { return _List.Count; }
    }

    public bool IsReadOnly
    {
        get { throw new NotImplementedException(); }
    }

    public int IndexOf(Weather item)
    {
        return _List.IndexOf(item);
    }

    public void Insert(int index, Weather item)
    {
        _List.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        _List.RemoveAt(index);
    }

    [XmlIgnore]
    public Weather this[int index]
    {
        get { return _List[index]; }
        set { _List[index] = value; }
    }

    #endregion

    public Weather Today
    {
        get
        {
            if (Count > 0)
            {
                return this[0];
            }

            throw new IndexOutOfRangeException("Could not retrieve todays weather from forecast");
        }
    }

    [XmlArray("simpleforecast")]
    [XmlArrayItem("forecastday")]
    public List<Weather> Forecast
    {
        get { return _List; }
        set { _List = value; }
    }
}

When I deserialize the feed, it doesn't seem to set the Forecast property so when I access Today, the exception is thrown. 当我反序列化提要时,似乎没有设置Forecast属性,因此当我访问Today时,将引发异常。 However, if I remove the inheritance from this class, it deserializes correctly. 但是,如果我从此类中删除继承,它将正确反序列化。

EDIT: So to reiterate, it appears as though it only deserializes correctly when ForecastCollection doesn't inherit from something which implements IEnumerable 编辑:因此可以重申,当ForecastCollection不能从实现IEnumerable的东西继承时,它似乎只能正确地反序列化。

Can anyone point out where I've gone wrong and the best way to fix this? 谁能指出我哪里出了问题以及解决此问题的最佳方法?

Thanks 谢谢

With a blank Weather class: 空白的Weather课:

[Serializable]
public class Weather
{
    // Nothing
}

following code works: 以下代码有效:

ForecastCollection collection = new ForecastCollection();
ForecastCollection collection2 = null;

collection.Add(new Weather());
collection.Add(new Weather());
collection.Add(new Weather());
collection.Add(new Weather());
collection.Add(new Weather());

XmlSerializer xs = new XmlSerializer(typeof(ForecastCollection));
using (Stream stream = new MemoryStream())
{
    xs.Serialize(stream, collection);
    stream.Seek(0, SeekOrigin.Begin);
    collection2 = (ForecastCollection)xs.Deserialize(stream);
}

Boolean success = (collection2.Count == 5); // True

我认为是因为每当您使用IEnumerable<T> ,编译器就会在后面生成一个迭代器类,并且该类不可serializable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM