【发布时间】:2013-04-20 22:19:41
【问题描述】:
GetEnumerator 强制转换为接口失败
没有编译错误
消息索引无穷大的运行时失败
如果我直接使用没有接口的 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; }
}
【问题讨论】:
标签: c# .net interface enumerator