堆栈溢出异常

本文关键字:异常 栈溢出 堆栈 | 更新日期: 2023-09-27 18:04:46

我有一个System.Windows.Forms.Treeview控件,带有复选框来构建权限树,节点是动态地从DB记录中构建的,以及从Allow Add, Allow Delete, Allow Update后面的代码添加的自定义节点。

问题是当我试图从树中检索检查节点时,我得到一个StackOverflowException

下面是我的代码:
List<tbl_JobPermission> SaveCheckedPermissions(int _JobID, System.Windows.Forms.TreeNodeCollection theNodes)
{
    using (WFMDBEntities _DBContext = new WFMDBEntities())
    {
        tbl_JobPermission _JopPermissionHelperVar;
        if (theNodes.Count > 0)
        {
            foreach (System.Windows.Forms.TreeNode aNode in theNodes.OfType<System.Windows.Forms.TreeNode>().Where(x => x.Checked == true))
            {
                int _tempJobPermID = int.Parse(aNode.Parent.Name);
                if (aNode.Name.Contains('_'))
                {
                    _JopPermissionHelperVar =
                            new tbl_JobPermission()
                            {
                                TblPremition = _tempJobPermID,
                                Tbljob = _JobID
                            };
                    if (aNode.Name.ToLower().Contains("add"))
                    {
                        _JopPermissionHelperVar.AllowNew = true;
                    }
                    else if (aNode.Name.ToLower().Contains("update"))
                    {
                        _JopPermissionHelperVar.AllowUpdate = true;
                    }
                    else if (aNode.Name.ToLower().Contains("delete"))
                    {
                        _JopPermissionHelperVar.AllowDelete = true;
                    }
                    if (!_JobPermissions.Contains(_JopPermissionHelperVar))
                    {
                        _JobPermissions.Add(_JopPermissionHelperVar);
                    }
                }
                else
                {
                    _JopPermissionHelperVar =
                            new tbl_JobPermission()
                            {
                                TblPremition = int.Parse(aNode.Name),
                                Tbljob = _JobID
                            };
                    if (!_JobPermissions.Contains(_JopPermissionHelperVar))
                    {
                        _JobPermissions.Add(_JopPermissionHelperVar);
                    }
                }
                if (aNode.Nodes.Count > 0)
                {
                    _JobPermissions.AddRange(SaveCheckedPermissions(_JobID, aNode.Nodes));
                }
            }
        }
    }
    return _JobPermissions;
}

堆栈溢出异常

这里有一个递归调用:

if (aNode.Nodes.Count > 0)
{
    _JobPermissions.AddRange(SaveCheckedPermissions(_JobID, aNode.Nodes));
}

在调试器下运行,检查在递归之前是否减少了节点的数量。

我认为如果所有的节点都在一个特定的层次上被检查,它将无限递归。

[编辑]

可能只是节点级别太多了。它在崩溃之前要递归多少层?你能用Debug.WriteLines来测试它吗?

我知道为什么我得到了StackOverFlow异常,问题不是在递归地调用该方法返回,我返回一个列表的数据获得每次调用该方法,列表项达到一个地步双打,所以我做很简单= =>我方法返回类型Void没有(因为我将填充列表列表(类中声明的范围)

void SaveCheckedPermissions(int _JobID, System.Windows.Forms.TreeNode RootNode)
{
    using (WFMDBEntities _DBContext = new WFMDBEntities())
    {
        tbl_JobPermission _JopPermissionHelperVar;
        foreach (System.Windows.Forms.TreeNode aNode in RootNode.Nodes)
        {
            if (aNode.Checked == true)
            {
                int _tempJobPermID;
                int.TryParse(aNode.Name, out _tempJobPermID);
                _JopPermissionHelperVar = new tbl_JobPermission();
                _JopPermissionHelperVar.Tbljob = _JobID;
                if (aNode.Name.Contains('_'))
                {
                    int _tempSpecialJobPermID;
                    int.TryParse(aNode.Parent.Name, out _tempSpecialJobPermID);
                    if (_JobPermissions.Where(x => x.TblPremition == _tempSpecialJobPermID).FirstOrDefault() != null)
                    {
                        _JobPermissions.Where(x => x.TblPremition == _tempSpecialJobPermID).FirstOrDefault().TblPremition = _tempSpecialJobPermID;
                        if (aNode.Name.ToLower().Contains("add"))
                        {
                            _JobPermissions.Where(x => x.TblPremition == _tempSpecialJobPermID).FirstOrDefault().AllowNew = true;
                        }
                        else if (aNode.Name.ToLower().Contains("update"))
                        {
                            _JobPermissions.Where(x => x.TblPremition == _tempSpecialJobPermID).FirstOrDefault().AllowUpdate = true;
                        }
                        else if (aNode.Name.ToLower().Contains("delete"))
                        {
                            _JobPermissions.Where(x => x.TblPremition == _tempSpecialJobPermID).FirstOrDefault().AllowDelete = true;
                        }
                    }
                }
                else
                {
                    if (_JobPermissions.Where(x => x.TblPremition == _tempJobPermID).FirstOrDefault() == null)
                    {
                        _JopPermissionHelperVar.TblPremition = _tempJobPermID;
                    }
                }
                if (_JobPermissions.Where(x => x.TblPremition == _JopPermissionHelperVar.TblPremition && x.Tbljob == _JopPermissionHelperVar.Tbljob).ToList().Count < 1)
                {
                    _JobPermissions.Add(_JopPermissionHelperVar);
                }
            }
            SaveCheckedPermissions(_JobID, aNode);
        }
    }
}