I have the a class Foo like this:
class Foo
{
public int id{get;set;}
public IEnumerable<Foo> Childs;
//some other properties
}
现在我想对一个Foo-Object及其所有子对象进行一些业务逻辑处理,如下所示:
public void DoSomeWorkWith(Foo x)
{
var firstItem = new {level = 0, item = x};
var s = new Stack<?>(?); //What type to use?
s.Push(firstItem);
while(s.Any())
{
var current = s.Pop();
DoSomeBusiness(current.item);
DoSomeMoreBusiness(current.item);
Log(current.level, current.item.id);
foreach(Foo child in current.item.Childs)
s.Push(new {level = current.level + 1, item = child});
}
}
我需要跟踪子级的(相对)级别/深度。 如何为匿名类型创建
Stack<T>
?当然,我可以创建一个简单的类来代替匿名类型(或更复杂的递归函数),但如何在不使用额外类的情况下解决此问题?