.Net CLR如何在内部实现"接口"?

Ami*_*itd 6 c# clr interface internals

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

Q1]当CLR遇到类似的情况时会发生什么:

简单的界面示例.(以下同样使用.)

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();

        }
    }
Run Code Online (Sandbox Code Playgroud)

Q2:在上面的例子中, (A)(B)如何区分?

问题3:通用接口的处理方式是否不同?

(当问这些基本问题时,感觉就像一个菜鸟......反正....)

大家好.

Gor*_*aci 2

这些代码实际上没有区别。两者最终都会调用相同的函数。通过类类型调用方法可能会带来较小的性能优势。

如果您想了解这些东西是如何实现的,请查看虚拟方法表

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