C#:访问链表的反向枚举数

| 我为LinkedList创建了一个“反向itearator”,现在我想将其与扩展方法一起使用:
public static class LinkedListExtensionMethods
{
    public static IEnumerator GetReverseEnumerator<T>(this LinkedList<T> linkedList)
    {
        return new LinkedListReverseEnumerator<T>(linkedList);
    }

    public static IEnumerator<T> GetReverseGenericEnumerator<T>(this LinkedList<T> linkedList)
    {
        return new LinkedListReverseEnumerator<T>(linkedList);
    }
}
但是,如果我写:
foreach (ICommand command in _CompoundDoCollection.GetReverseEnumerator<ICommand>())
它不起作用。 我该怎么办?     
已邀请:
那不是foreach的工作原理。任何实现IEnumerable接口的东西都必须重写GetEnumerator方法。这是foreach调用的方法。如果要向后枚举,则需要使自己的IEnumerable并使它的GetEnumerator返回ReverseEnumerator。您可以使用扩展方法来达到此目的,只需使扩展方法将您的LinkedList转换为ReverseLinkedList。     

要回复问题请先登录注册