以下代码在将.net更新为4.5.2
后给出了stackoverflowexceptionprivate string CalculateStringOrder(Page p, string s)
{
string res = null;
char pad = '0';
try
{
if (!String.IsNullOrEmpty(s))
{
**THIS GO IN STACK OVERFLOW**
res = p.ItemOrder.ToString().PadLeft(5, pad) + "." + s;
}
else
res = p.ItemOrder.ToString().PadLeft(5, pad);
_pagina = p;
if (_pagina.Parent == null)
return res;
while (_pagina.Parent != null)
{
res = CalculateStringOrder(_pagina.Parent, res);
}
//res = _stringOrder;
}
catch (StackOverflowException ex)
{ }
return res;
}
我已经尝试卸载更新清除所有引用,但错误仍然存在。它一直有效。
@UPDATE
这是堆栈跟踪:
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
BusinessLogic.dll!BusinessLogic.Page.CalculateStringOrder(BusinessLogic.Page p, string s) Riga 52 C#
> BusinessLogic.dll!BusinessLogic.Page.StringOrder.get() Riga 29 C#
BusinessLogic.dll!BusinessLogic.PageService.GetParentsTree.AnonymousMethod__2(BusinessLogic.Page x) Riga 40 C#
[Codice esterno]
BusinessLogic.dll!BusinessLogic.PageService.GetParentsTree() Riga 40 C#
adico.dll!adico.Manager.Topics.Topics() Riga 21 C#
[Codice esterno]
答案 0 :(得分:3)
在Page.Parent.Parent.Parent.etc中,您有循环参考。
例如p.Parent = p;
或p.Parent.Parent.Parent = p;
这会在您指定的行上给出堆栈溢出异常,但它是来自调试器的红色鲱鱼。
var p = new Page { ItemOrder = "a" };
p.Parent = p;
Console.WriteLine(CalculateStringOrder(p, "xx"));
一种解决方案是记录您已处理的页面,例如:
var processed = new List<Page>();
...
if (processed.Contains(p)) return res; // or throw an exception
processed.Add(p);
答案 1 :(得分:1)
我假设此代码尝试从树的底部或路径开始收集ItemOrder
个Page
个对象的值,然后处理到root代码很麻烦但是如果有的话会失败路径中的任何圆圈,即路径A -> B -> C-> A
将导致无限递归。当发生这种情况时,代码会尝试捕获不可避免的StackOverflowException并返回垃圾。
事实上,似乎有人试图混合使用递归和基于循环的代码来生成此路径。
如果生成路径的代码与生成字符串的代码分开,则更容易修复代码。以下代码将访问页面列表保存为@ freedomn-m建议:
private static Stack<Page> PathToTopRec(Page p, Stack<Page> visited=null )
{
visited = visited ?? new Stack<Page>();
if (visited.Contains(p))
return visited;
visited.Push(p);
if (p.Parent == null)
return visited;
return PathToTopRec(p.Parent, visited);
}
此方法将按从root到leaf的顺序返回所有父页面。然后,您可以使用以下命令生成路径字符串:
private static string CalculateStringOrder(Page p)
{
var path = PathToTopRec(p);
var builder=new StringBuilder();
foreach (var p in path)
{
builder.AppendFormat("{0:0000}.",p.ItemOrder);
}
builder.Remove(builder.Length- 1, 1);
return builder.ToString();
}
新方法将返回00001.00002.00003
以获取以下数据:
var p3 = new Page { ItemOrder = 3 };
var p2 = new Page { ItemOrder = 2 };
var p1 = new Page { ItemOrder = 1 };
p3.Parent = p2;
p2.Parent = p1;
var result=CalculateStringOrder(p3)
另一种选择是使用循环代替或递归:
private static IEnumerable<Page> PathToTop(Page p)
{
var visited = new List<Page>();
var current = p;
while (current != null)
{
yield return current;
visited.Add(current);
current = !visited.Contains(current.Parent)
? current.Parent
: null;
}
}
我使用迭代器,因此结果是IEnumerable,其页面与PathToTopRec
相同。对字符串生成代码的唯一更改是更改添加对Reverse()
的调用,因为迭代器将按照它们找到的顺序返回页面,从leaf到root。
var path = PathToTop(p).Reverse();
迭代器的优点是可以在其上应用任何LINQ运算符。实际上,字符串生成可能会改为:
var strings = PathToTop(p).Where(page=>page.ItemOrder>5)
.Select(page=>page.ItemOrder)
.Reverse()
.Select(it => string.Format("{0:0000}", it));
return String.Join(".", strings);
或LINQ运算符的任何其他组合。
原始代码混合了一个递归调用和一个while循环,这表明有人试图在某个时刻将递归代码转换为循环,但放弃了。