如何值类型实现REF类型 [英] How can a value type implement a ref type

查看:155
本文介绍了如何值类型实现REF类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到哪里值类型是实现Ref之情况。类型。

I came across a scenerio where a value type is implementing ref. type.

只是想知道怎么来是可能的(所发生的幕后?)

Just want to know how come is that possible ( what goes behind the scene ?)

一个结构是值类型和接口的Ref。类型,但一个结构可以实现界面没有任何错误...

A struct is a value type and interface a ref. type but a struct can implement interface without any error...

有什么想法?提前
谢谢

Any thoughts? Thanks in advance

推荐答案

事实上,它确实是在一次两种不同的方式。

Actually, it does it in two different ways at once.

首先,任何value-type可以装箱为基准类型的对象实例。此框由运行时发明了需求,将实现你期望的方式接口 - 即框将实现任何接口的值类型实现

Firstly, any value-type can be boxed into a reference-typed object instance. This box is invented by the runtime on demand and will implement the interface in the way you expect - i.e. the box will implement any interfaces the value-type implements.

不过, CLI中还允许呼叫受限。约束通话变成一个虚拟呼叫到一个静态调用,但只有在完全相同的地方的值类型实现通过重写或接口实现方式的实例方法的情况下(否则它是由JIT实现为虚拟呼叫)。尤其是仿制药大量使用受限的电话(同时作为仿制药加入受限码,就是出于这样的原因)的。

However, the CLI also allows for a "constrained call". A constrained call turns a virtual call into a static call but only in exactly to the scenario where a value type implements an instance method by way of override or interface implementation (otherwise it is implemented by the JIT as a virtual call). In particular generics makes extensive use of constrained calls (the constrained opcode was added at the same time as generics, for exactly this reason).

这篇关于如何值类型实现REF类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆