foreach (TargetObject innerObject1 in complexObject.InnerObjects) {
foreach (TargetObject innerObject2 in innerObject1.InnerObjects) {
foreach (TargetObject innerObject3 in innerObject2.InnerObjects) {
yield return innerObject3;
}
yield return innerObject2;
}
yield return innerObject1;
}
我该如何递归地做到这一点?我需要将所有嵌套列表中被包含的
TargetObject
实例汇总到一个列表中。下面是一个没有成功的尝试。 public static IEnumerable<TargetObject> GetRecursively(params TargetObject[] startingObjects)
{
foreach (TargetObject startingObject in startingObjects)
{
foreach (TargetObject innerObject in GetRecursively(startingObject.InnerObjects.ToArray()))
{
yield return innerObject;
}
yield return startingObject;
}
}