16,554
社区成员
发帖
与我相关
我的任务
分享
testxx f1 = new testxx(0);
testxx f2 = new testxx(0);
testxx f3=new testxx(0);
testxx f4=new testxx(0);
testxx f5 = new testxx(0);
f5.parent = f4;
f4.parent = f3;
f3.parent = f2;
f2.parent = f1;
//这一串初始过程,可以用代码实现,只是简单演示,我懒得写那么完备
for (int i = 0; i < 100; i++)
{
var res = f5.computeEx();
Console.WriteLine(string.Join(",",res));
}
-------------------------
对象
public class testxx
{
public testxx parent { get; set; }
public int value
{
get;
set;
}
public testxx(int init)
{
value = init;
}
public void compute()
{
value++;
if (value == 94)
{
value -= 94;
parent?.compute();
}
}
}
扩展方法
public static IEnumerable<int> computeEx(this testxx lst)
{
var obj = lst;
var res = lst.ParentValue();
foreach (var re in res)
{
yield return re;
}
obj.compute();
}
private static IEnumerable<int> ParentValue(this testxx obj)
{
if (obj.parent != null)
{
var b = obj.parent.ParentValue();
foreach (var i in b)
{
yield return i;
}
}
yield return obj.value;
}