Iterator blocks are rewritten by the compiler into classes that implement IEnumerable
or IEnumerator
; the compiler doesn't know how to generate a class that implement IDictionaryEnumerator
, so you can't use an iterator block to implement that interface.
A possible workaround is to provide your own implementation of IDictionaryEnumerator
:
class Hashtable : Dictionary<object, object>
{
new public IDictionaryEnumerator GetEnumerator()
{
return new DictionaryEnumerator(base.GetEnumerator());
}
struct DictionaryEnumerator : IDictionaryEnumerator
{
private Enumerator _en;
public DictionaryEnumerator(Dictionary<object, object>.Enumerator en)
{
_en = en;
}
public object Current
{
get
{
return Entry;
}
}
public DictionaryEntry Entry
{
get
{
var kvp = _en.Current;
return new DictionaryEntry(kvp.Key, kvp.Value);
}
}
public bool MoveNext()
{
bool result = _en.MoveNext();
return result;
}
public void Reset()
{
throw new NotSupportedException();
}
public object Key
{
get
{
var kvp = _en.Current;
return kvp.Key;
}
}
public object Value
{
get
{
var kvp = _en.Current;
return kvp.Value;
}
}
}
}