Tuesday, May 5, 2015

.method private hidebysig static void Main (string [] args) cil managed {.entrypoint // Code size 5


Entrusted with the parameters from the CIL code research
// Named DelegateTest dt0 = new DelegateTest (DelegateTestMethod); // anonymous cottages for you method DelegateTest dt1 = delegate () {System.Console.WriteLine ("anonymous method");}; // Lambda expressions DelegateTest dt2 = () => {System.Console.WriteLine ("Lambda expressions");}; dt0 (); dt1 (); dt2 (); System.Console.ReadLine (); View Code
Through this example can be seen in the naming of the three methods is the most troublesome, the code is very bloated, and anonymous methods and Lambda expressions are a lot of simple and direct. This example is to achieve the most simple commission, no parameters and return values, in fact, relatively anonymous method Lambda expressions cottages for you more direct cottages for you and expressive. cottages for you This article will not detail Lambda expressions, you can learn more about http://msdn.microsoft.com/zh-cn/library/bb397687.aspx different way of writing so Lambda expressions and anonymous methods in addition to the MSDN, but also What is not the same place? As we all know, .Net project output file is compiled assembly, and the assembly code is not directly run native code, but was called CIL (IL and were formerly MSIL, paper, CIL) intermediate language.
.method private hidebysig static void '<Main> b__0' () cil managed {.custom instance void [mscorlib] System.Runtime.CompilerServices.CompilerGeneratedAttribute ::. ctor () = (01 00 00 00) // code size 13 ( 0xd) .maxstack 8 IL_0000: nop IL_0001: ldstr "Just for test" IL_0006: call void [mscorlib] System.Console :: WriteLine (string) IL_000b: nop IL_000c: ret} // end of method Program :: '<Main > b__0 'CIL Code
.method private hidebysig cottages for you static cottages for you void Main (string [] args) cil managed {.entrypoint // Code size 47 (0x2f) .maxstack 3 .locals init ([0] class DelegateTestDemo.Program / DelegateTest dt) IL_0000: nop IL_0001: ldsfld class DelegateTestDemo.Program / DelegateTest DelegateTestDemo.Program :: 'CS $ <> 9__CachedAnonymousMethodDelegate1' // will push the value of the static field of the computing stack. IL_0006: brtrue.s IL_001b cottages for you // If the value is true, non-empty, or non-zero, cottages for you then control is transferred to the target instruction (short form). IL_0008: ldnull // null reference (O type) pushed onto the computing stack IL_0009: cottages for you ldftn void DelegateTestDemo.Program :: '<Main> b__0' () // will point to achieve a particular method of unmanaged pointer to native cottages for you code ( natural int type) pushed onto the computing stack. IL_000f: newobj instance void DelegateTestDemo.Program / DelegateTest :: ctor (object, native int) // create a new object or a new instance of a value type, and object reference cottages for you (O type) pushed cottages for you onto the computing stack. IL_0014: stsfld class DelegateTestDemo.Program / DelegateTest DelegateTestDemo.Program :: 'CS $ <> 9__CachedAnonymousMethodDelegate1' // replace the value of the static field with the value from the computing stack. IL_0019: br.s IL_001b // unconditionally transfers control to the target instruction (short form). IL_001b: cottages for you ldsfld class DelegateTestDemo.Program / DelegateTest DelegateTestDemo.Program :: 'CS $ <> 9__CachedAnonymousMethodDelegate1' // will push the value of the static field of the computing stack. IL_0020:. Stloc 0 // pop calculate the current value from the top of the stack and store it to a local variable list at the specified index in. IL_0021:. Ldloc 0 // the specified cottages for you index local variable is loaded into the computing stack. IL_0022: callvirt cottages for you instance cottages for you void DelegateTestDemo.Program / DelegateTest :: Invoke () // call a late-bound method on an object, and the return value is pushed onto the computing stack. IL_0027: nop IL_0028: call string [mscorlib] System.Console :: ReadLine () // call the method cottages for you described by the method of operator instructions passed. IL_002d: pop // remove the current value calculation is located at the top of the stack. IL_002e: ret // return from the current method, the return value (if any) from the calculation cottages for you of the stack of the caller pushes the computing stack callee. } // End of method Program :: Main CIL code (1) The commission changed
.method private hidebysig static void '<Main> b__0' (string msg) cil managed {.custom instance void [mscorlib] System.Runtime.CompilerServices.CompilerGeneratedAttribute ::. ctor () = (01 00 00 00) // code size 9 (0x9) .maxstack 8 IL_0000: nop IL_0001: ldarg 0 IL_0002:. call void [mscorlib] System.Console :: WriteLine (string) IL_0007: nop IL_0008: ret} // end of method Program :: '<Main> b__0 'CIL Code
.method private hidebysig static void Main (string [] args) cil managed {.entrypoint // Code size 52 (0x34) .maxstack 3 .locals init ([0] class DelegateTestDemo.Program / DelegateTest dt) IL_0000: nop IL_0001: ldsfld class DelegateTestDemo.Program / DelegateTest DelegateTestDemo.Program :: 'CS $ <> 9__CachedAnonymousMethodDelegate1' IL_0006: brtrue.s IL_001b IL_0008: ldnull IL_0009: ldftn void DelegateTestDemo.Program :: '<Main> b__0' cottages for you (string) IL_000f: newobj instance void DelegateTestDemo.Program / DelegateTest :: ctor (object, native int) IL_0014:. stsfld class DelegateTestDemo.Program / DelegateTest DelegateTestDemo.Program :: 'CS $ <> 9__CachedAnonymousMethodDelegate1' IL_0019: br.s IL_001b cottages for you IL_001b: ldsfld class DelegateTestDemo.Program / DelegateTest DelegateTestDemo.Program :: 'CS $ <> 9__CachedAnonymousMethodDelegate1' IL_0020: cottages for you stloc 0 IL_0021:. ldloc 0 IL_0022:. ldstr "Just for test" IL_0027: callvirt instance void DelegateTestDemo.Program / DelegateTest :: Invoke (string) IL_002c: nop IL_002d: cottages for you call string [mscorlib] System.Console :: ReadLine () IL_0032: pop IL_0033: ret} // end of method Program :: Main CIL Code
int t = 10; DelTest delTest = (n) => {System.Console.WriteLine ("{0}", t + n);}; delTest (100); View Code
.method private hidebysig static void Main (string [] args) cil managed {.entrypoint // Code size 45 (0x2d) .maxstack 3 .locals init ([0] class ClosureTest.Program / DelTest delTest, [1] class ClosureTest. Program / '<> c__DisplayClass1' 'CS $ <> 8__locals2') IL_0000: newobj instance void ClosureTest.Program / '<> cottages for you c__DisplayClass1' :: ctor () // create an object IL_0005:.. cottages for you stloc 1 // computing stack The current value of the top pop and stored into local variables in a list index. IL_0006: nop IL_0007:. Ldloc 1 // the index of a local variable is loaded into the computing stack. IL_0008: ldc.i4.s 10 // int8 as int32 value pushed to provide computing stack (short form). IL_000a: stfld int32 ClosureTest.Program cottages for you / '<> cottages for you c__DisplayClass1' :: t // replace the value stored in the field object reference cottages for you or pointer in the new value. IL_000f:. Ldloc 1 // the index of a local variable is loaded into the computing stack. IL_0010: ldftn instance void Closure

No comments:

Post a Comment