【发布时间】:2010-08-07 07:27:30
【问题描述】:
我有这样的 Foo 类:
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>?当然我可以创建一个简单的类而不是匿名类型(或更复杂的递归函数),但是如何在没有额外类的情况下解决这个问题呢?
【问题讨论】:
-
您能否格式化您的代码:)
-
Childs读起来有点奇怪,因为 'child' 的英文复数是 'children' :)
标签: c# .net anonymous-types