stsfld 与 stfld
查看 stfld 和 stsfld il 操作码之间的差异,stfld 有空引用检查,而 stsfld 没有。这是为什么呢?是因为静态字段位于高频堆上,所以它们不会被垃圾回收吗?
http://msdn.microsoft.com/library/system.reflection .emit.opcodes.stfld.aspx
堆栈转换行为(按顺序)是:
- 对象引用或指针被推入堆栈。
- 一个值被压入堆栈。
- 值和对象引用/指针从 堆;对象中字段的值被替换为提供的 值。
stfld指令替换对象字段的值(类型 O) 或通过具有给定值的指针(类型 native int、& 或 *)。 Field 是引用字段成员引用的元数据标记。这 stfld 指令可以具有 Unaligned 和 Unaligned 中的一个或两个前缀 易挥发。
如果对象引用或指针被抛出,则抛出NullReferenceException 空引用并且该字段不是静态的。
如果在元数据中找不到字段,则会抛出 MissingFieldException。 当 Microsoft 中间语言 (MSIL) 指令转换为本机代码,而不是在运行时。
Looking at the difference between the stfld and stsfld il op codes, the stfld has a null reference check while stsfld does not. Why is this? Is it because static fields are on the high-frequency heap and so they are not garbage collected?
http://msdn.microsoft.com/library/system.reflection.emit.opcodes.stfld.aspx
The stack transitional behavior, in sequential order, is:
- An object reference or pointer is pushed onto the stack.
- A value is pushed onto the stack.
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
静态成员从不有目标实例。如果有参数(在方法上),则 arg0 指的是第一个参数,而不是目标实例(又名
this
)。由于没有目标实例,空检查是没有意义的:没有什么可以取消引用。Static members never have a target instance. If there are parameters (on a method), arg0 refers to the first parameter, not the target instance (aka
this
). Since there is no target-instance, a null check is meaningless: there is nothing to dereference.