Давайте у меня есть два класса:C# получить уровень вложенности дерева
public Foo
{
public List<Foo> Childs { get; set; }
public Bar BarObj { get; set; }
public int Level { get; set; }
}
public Bar
{
public List<Foo> Childs { get; set; }
}
Теперь я хочу, чтобы получить вложенности уровня из коллекции «Foo» Объекты
мой текущий метод работы выглядит следующим образом:
int currentLevel = 0;
public void ApplyNestingLevel(List<Foo> list)
{
foreach(var item in list)
{
item.Level = currentLevel;
if(item.Childs.Count > 0 || item.BarObj.Childs.Count > 0)
{
currentLevel++;
}
ApplyNestingLevel(item.Childs);
ApplyNestingLevel(item.BarObj.Childs);
}
}
Как я могу сделать это более «элегантным/простым»?
Может быть, вы могли прочитать о посетителя шаблон http://en.wikipedia.org/wiki/Visitor_pattern – wudzik
я не вижу свойство «Else» в Foo. – Clinton
oops sorry my bad ... typo – makim