【问题标题】:IEnumerator IssueIEnumerator 问题
【发布时间】:2017-11-03 01:18:22
【问题描述】:

我想列举一个菜单,但我似乎无法一直到孩子们。

我的模型是这样的。

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 进行一些安全检查,但我永远无法获得子值。

我知道我可以使用 linq 来执行此操作,但我的想法是拆分安全代码检查,以免污染我的 foreach 语句。

foreach (var menuItem in menu)
{

}

很难解释我的问题以及我想要完成的工作

【问题讨论】:

  • 您在寻找yield return吗?
  • 为什么不把MenuEnumerator 封装成Children.GetEnumerator() 返回的枚举数呢?转接所有来电。
  • 您要返回菜单本身 + 所有子项和子项吗?
  • @evk,是的,这听起来很正确

标签: c# ienumerable ienumerator


【解决方案1】:

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

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;
            }
        }
    }
}

【讨论】:

  • 啊完美,我想我是想把事情复杂化
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
相关资源
最近更新 更多