IL 中的 if 是什么样的?
if
语句编译成 IL 后会是什么样子?
这是 C# 中非常简单的构造。有人能给我一个更抽象的定义吗?
What does an if
statement look like when it's compiled into IL?
It's a very simple construct in C#. Can sombody give me a more abstract definition of what it really is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是一些
if
语句以及它们如何转换为 IL:这里需要注意的一件事:IL 指令始终是“相反的”。
if (i > 0)
转换为有效的意思是“如果i <= 0
,则跳过if
的主体堵塞”。Here are a few
if
statements and how they translate to IL:One thing to note here: The IL instructions are always the “opposite”.
if (i > 0)
translates to something that effectively means “ifi <= 0
, then jump over the body of theif
block”.使用分支指令,根据堆栈顶部的值跳转到目标指令。
A branch instruction is used that will jump to a target instruction depending on the value(s) on top of the stack.
这取决于
if
的条件。例如,如果您正在检查null
的引用,编译器将发出brfalse
指令(或brtrue
取决于您所写的内容)。实际
if
条件将根据条件本身而有所不同,但像ILDASM
或 Reflector 这样的反汇编器将是了解更多信息的更好工具。It depends on the condition of the
if
. For example if you are checking a reference againstnull
the compiler will emit abrfalse
instruction (or abrtrue
depending on what you wrote).The actual
if
condition will differ based on the condition itself but a dissasembler likeILDASM
or Reflector would be a better tool for learning more.一个简单的例子:
这将等于
其他 if 会有所不同,包括条件分支。这确实太多了,无法在一篇文章中解释,您最好寻找 IL-Code 的介绍。
codeproject 上有一篇关于此的好文章。
A simple example:
This would be equal to
Other ifs would differ, including conditional branches. This really is too much to explain in one post, you are better of looking for an introduction to IL-Code.
There is a nice article on codeproject concerning this.