.net - stsfld 与 stfld

标签 .net il

查看 stfld 和 stsfld il 操作码之间的区别,stfld 具有空引用检查,而 stsfld 没有。为什么是这样?是因为静态字段在高频堆上,所以它们没有被垃圾收集吗?

http://msdn.microsoft.com/library/system.reflection.emit.opcodes.stfld.aspx

The stack transitional behavior, in sequential order, is:

  1. An object reference or pointer is pushed onto the stack.
  2. A value is pushed onto the stack.
  3. The value and the object reference/pointer are popped from the stack; the value of field in the object is replaced with the supplied value.

The stfld instruction replaces the value of a field of an object (type O) or via a pointer (type native int, &, or *) with a given value. Field is a metadata token that refers to a field member reference. The stfld instruction can have a prefix of either or both of Unaligned and Volatile.

NullReferenceException is thrown if the object reference or pointer is a null reference and the field isn't static.

MissingFieldException is thrown if field is not found in the metadata. This is typically checked when the Microsoft Intermediate Language (MSIL) instruction is converted to native code, not at runtime.

最佳答案

静态成员从不 有一个目标实例。如果有参数(在方法上),arg0 指的是第一个参数,而不是目标实例(aka this)。由于没有目标实例,空检查是没有意义的:没有什么可以取消引用。

关于.net - stsfld 与 stfld,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6999970/

相关文章:

.net - CIL unbox_any 指令 - 奇怪的行为

.net - ASP.NET 根据角色拒绝访问某些页面

.net - 是否有办法确定 Windows 进程中可用的剩余内存量?

c# - 调用 timer.Stop() 后执行的 System.Timers.Timer Elapsed 事件

c# - 在没有 Web 应用程序的情况下使用 Amazon Elastic Beanstalk 部署 .NET Windows 服务

c# - [opt] 在 MSIL 中是什么意思?

c# - 如何加载静态字段?

.net - 防御性反多线程类实现

c# - WCF 不活动超时

c# - 加载常量的 MSIL 指令