GetEnumerator cast to an Interface failed
没有编译器错误
运行时失败,消息索引无穷大
如果我直接使用 struct Word1252 而没有接口,它可以工作
namespace WordEnumerable
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WordEnum wordEnum = new WordEnum();
Debug.WriteLine(wordEnum[0].GetType().ToString());
Debug.WriteLine(wordEnum[0].Value);
Debug.WriteLine(wordEnum.Count.ToString());
foreach (iWord w in wordEnum) // fails here
{
}
}
}
}
public interface iWord
{
Int32 Key { get; }
String Value { get; }
}
public class WordEnum : IEnumerable<iWord>
{
private static List<Word1252> words1252 = new List<Word1252>();
IEnumerator<iWord> IEnumerable<iWord>.GetEnumerator()
{
return ((IEnumerable<iWord>)words1252).GetEnumerator();
}
public struct Word1252 : iWord
{
public UInt64 packed;
public Int32 Key { get { return (Int32)((packed >> 27) & ((1 << 25) - 1)); } }
public Byte Length { get { return (Byte) ((packed >> 59) & ((1 << 5) - 1)); } }
public String Value { get { return Key.ToString(); } }
public Word1252(UInt64 Packed) { packed = Packed; }
}