IEnumerator 问题

IEnumerator Issue

我想通过菜单进行枚举,但我似乎无法一直到 children。

我的模型是这样的。

public class Menu : IEnumerable<Menu>
{
    public new IEnumerable<Menu> Children { get; set; }

    public IEnumerator<Menu> GetEnumerator()
    {
        return new MenuEnumerator(this);
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public class MenuEnumerator : IEnumerator<Menu>
{
    private readonly Menu _menu;

    public MenuEnumerator(Menu menu)
    {
        _menu = menu;
    }

    public bool MoveNext()
    {
        if (this.Current == null)
        {
            this.Current = this._menu;
        }

        return true;
    }

    public void Reset()
    {

    }

    public Menu Current { get; set; }

    object IEnumerator.Current => Current;

    public void Dispose()
    {
        this.Current = null;
    }
}

我的菜单结构

public static IEnumerable<Menu> GetMenuStructure()
{
    return new Menu
    {
        Children = new List<Menu>
        {
            new Menu
            {
                PkLinkId = 2,
                FkParentLinkId = null,
                Target = "_self",
                Text = "Company",
                Children = new List<Menu>()
                {
                    new Menu()
                    {
                        PkLinkId = 4,
                        FkParentLinkId = 2,
                        Target = "_self",
                        Text = "Add A New Company",

                    },
                    new Menu()
                    {
                        PkLinkId = 27,
                        FkParentLinkId = 2,
                        Target = "_self",
                        Text = "Basic Company Information",
                    },
                }
            },
            new Menu()
            {
                PkLinkId = 5,
                FkParentLinkId = null,
                Target = "_self",
                Text = "Bureau",

                Children = new List<Menu>()
                {
                    new Menu()
                    {
                        PkLinkId = 31,
                        FkParentLinkId = 5,
                        Target = "_self",
                        Text = "Admin",
                    },
                    new Menu()
                    {
                        PkLinkId = 76,
                        FkParentLinkId = 5,
                        Target = "_self",
                    },
                }
            }
        }
    };
}

每次迭代我都想进入 MoveNext 函数,这样我就可以对 menuitem 进行一些安全检查,但我永远无法获得 child 值。

我知道我可以使用 linq 来做到这一点,但我的想法是拆分安全代码检查,这样我就不会污染我的 foreach 语句。

foreach (var menuItem in menu)
{

}

很难解释我的问题和我想完成的事情

如果你只想展平层次结构并枚举整个菜单,你可以这样做:

public IEnumerator<Menu> GetEnumerator()
{            
    // first return self
    yield return this;
    if (Children != null) {                
        foreach (var child in Children) {
            // recursively call this same function of each child
            foreach (var subChild in child) {
                yield return subChild;
            }
        }
    }
}