IL:ldfld 与 ldflda
我正在使用 Mono.Cecil 编写一个小型 IL 编织应用程序,这需要我在 IL 级别操作目标程序集。
我的问题很简单,但我还是觉得这件事很令人困惑。 ldfld 和 ldflda
指令之间的实际区别是什么?
我查阅了 msdn,看起来虽然 ldfld
code> 获取字段的值,ldflda
获取字段的地址。好吧……但这是什么意思呢?我首先认为前者用于值类型(和字符串),后者用于引用类型,但我编译了一个 C# 代码片段并在 Reflector 中检查了它,结果证明我错了。
当 C# 编译器发出 ldflda
而不是 ldfld
时,我无法找到任何不同的模式,并且我的搜索没有显示任何可以解释这一点的文章。那么什么时候使用ldflda
而不是ldfld
呢?
任何帮助将非常感激。
I'm writing a small IL-weaving application using Mono.Cecil, that requires me to manipulate the target assembly on an IL level.
My question is quite simple, but I still find the matter very confusing. What is the practical difference between the ldfld
and ldflda
instructions?
I have consulted with msdn, and it appears that while ldfld
fetches the value of the field, ldflda
gets the address of the field. Okay... but what does this mean? I first thought that the former is used for value types (and string), and the latter for reference types, but I have compiled a C# snippet and checked it in Reflector, and it proved me wrong.
I haven't been able to find any distinct pattern in when the C# compiler emits ldflda
instead of ldfld
, and my searches didn't reveal any articles that would explain this. So when to use ldflda
instead of ldfld
?
Any help would be very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜它用于
struct
ures:我很确定这里的
c
是按地址加载的,否则它将创建Counter
的副本。这就是为什么你不能正确地做到这一点。I guess it is used for
struct
ures:I am pretty sure
c
here is loaded by address otherwise it would create a copy ofCounter
. That's why you can't do this with propery.如果将字段作为 ref 参数传递给子例程,您应该看到编译器为其发出 ldflda,以便获取要传入的引用。
If you pass a field to a subroutine as a ref parameter, you should see the compiler emit a ldflda for it in order to obtain the reference to pass in.