IL:ldfld 与 ldflda

发布于 2024-11-04 07:24:13 字数 484 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

困倦 2024-11-11 07:24:13

我猜它用于structures:

struct Counter
{
   public int i;
}

struct Something
{
   public Counter c;
}

Something s;
s.c.i++;

我很确定这里的c是按地址加载的,否则它将创建Counter的副本。这就是为什么你不能正确地做到这一点。

I guess it is used for structures:

struct Counter
{
   public int i;
}

struct Something
{
   public Counter c;
}

Something s;
s.c.i++;

I am pretty sure c here is loaded by address otherwise it would create a copy of Counter. That's why you can't do this with propery.

耳钉梦 2024-11-11 07:24:13

如果将字段作为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文