如何.NET CLR实施"接口与QUOT;在内部? [英] How does .Net CLR implement an "Interface" internally?

查看:106
本文介绍了如何.NET CLR实施"接口与QUOT;在内部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是好奇.NET CLR如何在内部处理接口

Just curious about how .NET CLR handles interfaces internally?

Q1]当CLR遇到喜欢的东西会发生什么:

Q1] What happens when CLR encounters something like :

简单的接口例子。(下同使用)

interface ISampleInterface
    {
        void SampleMethod();
    }

    class ImplementationClass : ISampleInterface
    {
        // Explicit interface member implementation: 
        public void SampleMethod()
        {
            // Method implementation.

        }

        static void Main()
        {
            //Declare an interface instance.
            ISampleInterface mySampleIntobj = new ImplementationClass();  // (A)
           // Call the member.
            mySampleIntobj.SampleMethod();

            // Declare an interface instance. 
            ImplementationClass myClassObj = new ImplementationClass();  // (B)
           //Call the member.
            myClassObj.SampleMethod();

        }
    }



Q 2:在上面的例子如何是的(A)(b)区别

Q3:是否的通用接口区别对待?

Q3 : Are Generic Interfaces treated differently?

(感觉像问一个菜鸟的时候基本问题像这样的......反正....)

(Feel like a noob when asking basic questions like these ...anyways....)

THX所有。

推荐答案

有在这些代码位几乎没有区别。双方最终调用相同的功能。有可能是在呼唤通过类类型的方法轻微的性能优势。

There are practically no differences in those bits of code. Both end up calling the same function. There may be minor performance benefits in calling the method through the class type.

如果你想这些东西是如何实现的,看看的虚拟方法表

If you want to how these stuff are implemented, have a look at Virtual Method Tables.

有关更深入的信息,请参阅的这个

For deeper information, see this.

这篇关于如何.NET CLR实施"接口与QUOT;在内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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