题
考虑这个C#代码段:
static string input = null;
static string output = null;
static void Main(string[] args)
{
input = "input";
output = CallMe(input);
}
public static string CallMe(string input)
{
output = "output";
return output;
}
使用反射显示Dissassembling:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.maxstack 8
L_0000: nop
L_0001: ldstr "input"
L_0006: stsfld string Reflector_Test.Program::input
L_000b: ldsfld string Reflector_Test.Program::input
L_0010: call string Reflector_Test.Program::CallMe(string)
L_0015: stsfld string Reflector_Test.Program::output
L_001a: ret
}
.method public hidebysig static string CallMe(string input) cil managed
{
.maxstack 1
.locals init (
[0] string CS$1$0000)
L_0000: nop
L_0001: ldstr "output"
L_0006: stsfld string Reflector_Test.Program::output
L_000b: ldsfld string Reflector_Test.Program::output
L_0010: stloc.0
L_0011: br.s L_0013
L_0013: ldloc.0
L_0014: ret
}
这困惑我的片是:
L_0010: stloc.0
L_0011: br.s L_0013
L_0013: ldloc.0
它存储该项目时,分支到下一行(这将被执行反正),然后再次装入。
是否有一个原因?
解决方案
这仅发生在调试,而不是在释放。我怀疑其调试期间协助。它也许可以让你扔断点Mid语句,看到了返回值。
请注意发行版本具有更简洁IL:
.method private hidebysig static void Main(string[] args) cil managed
{
.maxstack 8
L_0000: ldstr "input"
L_0005: stsfld string Reflector_Test.Program::input
L_000a: ldsfld string Reflector_Test.Program::input
L_000f: call string Reflector_Test.Program::CallMe(string)
L_0014: stsfld string Reflector_Test.Program::output
L_0019: ret
}
.method public hidebysig static string CallMe(string input) cil managed
{
.maxstack 8
L_0000: ldstr "output"
L_0005: stsfld string Reflector_Test.Program::output
L_000a: ldsfld string Reflector_Test.Program::output
L_000f: ret
}
其他提示
我的猜测是,这是执行return语句样板代码,编译器执行无条件跳转到最后一行,并加载返回值到寄存器执行ret
之前。该JIT将更好地优化它,我觉得编译器不打扰做任何的优化。
不隶属于 StackOverflow