当你把一个简单的类.
public sealed class C {
public static void M() {
}
}
变成https://sharplab.io/
它翻译成(带我的注释):(source)
C.M()
L0000: push ebp /////////////////////
L0001: mov ebp, esp // function frame initialization
L0003: push edi /////////////////////
L0004: cmp dword ptr [0x281dc19c], 0 // if (0 == ???)
L000b: je short L0012 // then: jump to actual method body
L000d: call 0x727a8790 // else: call ??? what ???
L0012: nop // the actual method body
L0013: nop // the actual method body
L0014: pop edi /////////////////////
L0015: pop ebp // function frame teardown/exit
L0016: ret /////////////////////
L0004到L000d的用途是什么?
L0004: cmp dword ptr [0x281dc19c], 0 // if (0 == ???)
L000b: je short L0012 // then: jump to actual method body
L000d: call 0x727a8790 // else: call ??? what ??
What is the called function?
Is it terminating the process?
Why does C# JIT put this in every method?
我以为它可能与继承有关,但我密封了类并使方法成为静态的,以消除该选项.
这是某种理智的判断吗?就像支票一样:
- 方法重载
- 代码损坏
- 堆栈溢出
- 分段故障
IL没有给出任何线索:
.method public hidebysig static
void M () cil managed
{
// Method begins at RVA 0x2069
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
}
最新情况:
Thx@dai,将其设置为Release将删除代码.为了确保完全删除不是由空方法主体引起的,我添加了一条简单的语句
public static void M() {
System.Console.WriteLine(7);
}
发布模式下的JIT结果为(source):
C.M()
L0000: mov ecx, 7
L0005: call dword ptr [0x10a25768]
L000b: ret
就像@Dai说的那样,没有提到cmp-je