我有这个对象:
class Animation
{
//[...]
private SortedList<int,Frame> frames = new SortedList<int,Frame>();
private IDictionaryEnumerator frameEnumerator = null;
//[...]
public void someFunction() {
frameEnumerator = frames.GetEnumerator(); //throw error
}
//[...]
}
我在那里查看 msn 文档:http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.getenumerator.aspx ,看起来我的代码是正确的,但 VS 说:
cannot convert System.Collections.Generic.IEnumerator>' to 'System.Collections.IDictionaryEnumerator'.
请您参考如下方法:
IDictionaryEnumerator type 用于较旧的非通用集合类型。在这种情况下,您有一个强类型集合,它将返回 IEnumerater<KeyValuePair<int, Frame>> .请改用该类型
private IEnumerator<KeyValuePair<int, Frame>> frameEnumerator = null;
注意:SortedList<TKey, TValue> 的枚举器类型确实实现了 IDictionaryEnumerator界面。如果您真的喜欢那个,您可以通过显式转换访问它
frameEnumerator = (IDictionaryEnumerator)frames.GetEnumerator();
不过我会避开这条路线。最好使用强类型接口(interface)并避免在代码中进行不必要的转换。
