使用C#的Windows phone 7.5类型';System.InvalidCastException'

本文关键字:System InvalidCastException 5类型 Windows phone 使用 | 更新日期: 2023-09-27 18:00:12

有人知道强制转换itemCollection.GetEnumerator()的正确方法吗?

由于某些原因,返回itemCollection.GetEnumerator();在windowsphone 8中运行良好,但在windowsphone 7.5中不起作用。它要求我明确地投射它,但我不确定什么是正确的方式。有人知道吗?

public class ItemCollection : IEnumerable<Object>
{
    private System.Collections.ObjectModel.ObservableCollection<Item> itemCollection = new System.Collections.ObjectModel.ObservableCollection<Item>();
    public IEnumerator<Object> GetEnumerator()
    {
        //return itemCollection.GetEnumerator();
        IEnumerator<Object> test = (IEnumerator<Object>)itemCollection.GetEnumerator();
        return test;
    }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
    public void Add(Item item)
    {
        itemCollection.Add(item);
    }
}

使用C#的Windows phone 7.5类型';System.InvalidCastException'

我不确定,但试试这个

IEnumerator<Object> test = new IEnumerator<Object>(itemCollection.GetEnumerator());

也可以试试这个

return itemCollection.Cast<object>()

嗯,我不清楚你为什么要这样做:这样做不是更好吗:

public class ItemCollection : System.Collections.ObjectModel.ObservableCollection<Item>
{
}