.NET, Architecture, ALM, Cloud
C# Code :
Action<int> loopSample = cpt =>
{
for (int i = 0; i < cpt; i++)
Console.WriteLine(i);
};
Expression Tree Code :
Expression<Action<int>> loopSampleE =
// Action<int> loopSample =
Expression.Lambda<Action<int>>(
//{
Expression.Block(
//i = 0
new[] { iVariable },
Expression.Assign(
iVariable,
Expression.Constant(0)),
//while(true)
Expression.Loop(
//{
Expression.Block(
//if
Expression.IfThen(
//!(i < cpt)
Expression.Not(
Expression.LessThan(iVariable, cptParam)),
//break
Expression.Break(breakOuter)),
//Console.WriteLine(i)
Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), iVariable),
//i++
Expression.PostIncrementAssign(iVariable)),
breakOuter)),
cptParam);
Expression Tree Debug View :
.Lambda #Lambda1<System.Action`1[System.Int32]>(System.Int32 $cpt) {
.Block(System.Int32 $i) {
$i = 0;
.Loop {
.Block() {
.If (
!($i < $cpt)
) {
.Break #Label1 { }
} .Else {
.Default(System.Void)
};
.Call System.Console.WriteLine($i);
$i++
}
}
.LabelTarget #Label1:
}
}
IL Code :
//i = 0
IL_0000: ldc.i4.0
IL_0001: stloc.0
//i < cpt
IL_0002: ldloc.0
IL_0003: ldarg.1
IL_0004: clt
//Condition Valid : --> go to WriteLine Block
IL_0006: brtrue IL_0010
//Condition InValid--> ret
IL_000b: br IL_0021
//Console.WritleLine(i)
IL_0010: ldloc.0
IL_0011: call 6000003 System.Console.WriteLine(Int32)
//i++
IL_0016: ldloc.0
IL_0017: stloc.1
IL_0018: ldloc.1
IL_0019: ldc.i4.1
IL_001a: add
IL_001b: stloc.0
//loop
IL_001c: br IL_0002
IL_0021: ret