I was wondering if someone could explain this to me. For some reason the MethodBody of a lambda seems to change after invocation.
The following code snippet makes a simple lambda with one outer variable and examines the method body.
int x = 7;
Func<int> f = () => x;
int before = f.Method.GetMethodBody().GetILAsByteArray().Length;
f();
int after = f.Method.GetMethodBody().GetILAsByteArray().Length;
In this case the length before invocation is 11 bytes and after invocation is 126.
ILDASM shows the lambda body in the DLL (before case) as 11 bytes with the expected IL:
IL_0000: ldarg.0
IL_0001: ldfld int32 Tests.LambdaTest::x
IL_0006: stloc.0
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000a: ret
I have not had time to try and disassembled the 'after' case as (presumably) I will have to extract the IL at run-time to feed into a disassembler. I do know it does contain the relevant ldfld. Probably if I did go to the trouble it may be obvious (?)
This is not critical for me, just interested to understand what is going on.
regards,
Andrew