我现在正在使用 IL 代码,以后需要自己编写。由于误解,我有一些担忧。这是 C# 中的一个简单方法
public static string Method1(int id)
{
return Method2(id);
}
这是它的 IL 代码
.method public hidebysig static string
Method1(int32 id) cil managed
{
//
.maxstack 1
.locals init ([0] string CS$1$0000)
IL_0000: nop // Why?
IL_0001: ldarg.0
IL_0002: call string MyNamespace.MyClass::Method2(int32)
IL_0007: stloc.0 // storing a return value of MyClass::Method2 to local variable.
IL_0008: br.s IL_000a // Why?
IL_000a: ldloc.0 // Does it really require and why?
IL_000b: ret
} // end of method MyClass::Method1
由于某种原因,CIL 中的每个 方法都有nop
操作。为什么它有它?
在我的例子中,是否有必要使用 br.s IL_000a
并且没有它它会工作吗?
最佳答案
nop 允许您调试(设置断点),如果您在 Release模式下编译,您将在代码中看到更少的 nope。
如果您在发布/优化模式下编译相同的代码,IL 应该看起来更清晰:
.method public hidebysig static string Method1(int32 id) cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: call string MyNamespace.MyClass::Method2(int32)
L_0006: ret
}
关于c# - MSIL代码调查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12856155/