Hi people, I have the a class Foo like this:
class Foo
{
public int id{get;set;}
public IEnumerable<Foo> Childs;
//some other properties
}
Now I want to process some business logic on a Foo-Object and all it's children like this:
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});
}
}
I need to keep track of the (relative) level/depth of the childs.
How do I create a Stack<T>
for the anonymous type? Of course I could create a simple class instead of the anonymous type (or a more complicated recursive function), but how to solve this problem without an additional class?