简体   繁体   English

CLR如何加载接口类型?

[英]How are interface types loaded by the CLR?

Does the CLR Load interface types if they are not initialized with Concrete implementations? 如果未使用具体实现初始化CLR加载接口类型,是否可以?

Considering an interface IFoo which has an implementation FooImpl 考虑具有实现FooImpl的接口IFoo

eg 例如

IFoo foo;

as opposed to 相对于

IFoo foo = new FooImpl();

Will the CLR only load the IFoo Type in the second case? 在第二种情况下,CLR是否只会加载IFoo类型? Also if there is a another type which inherits from FooImpl (eg FooImpl2) 另外,如果还有另一个继承自FooImpl的类型(例如FooImpl2)

Will this code start from FooImpl2 and walk up the inheritance hierarchy to load IFoo interface? 此代码是否将从FooImpl2开始并沿继承层次结构加载IFoo接口? Also, will the IFoo's MethodTable contain a pointer directly to the FooImpl2's method table or will it redirected via the intermediate implementing type (ie FooImpl). 另外,IFoo的MethodTable将直接包含指向FooImpl2的方法表的指针,还是通过中间实现类型(即FooImpl)重定向。

IFoo foo = new FooImpl2();

This line of code 这行代码

IFoo foo;

says that the foo is a variable that can hold a reference to an object that implements the interface IFoo . 表示foo是一个变量,可以保存对实现接口IFoo的对象的引用。

Will the CLR only load the IFoo Type in the second case? 在第二种情况下,CLR是否只会加载IFoo类型?

In the second case you create an object of type FooImpl and you make use of a variable called foo that stores a reference to this object. 在第二种情况下,您将创建一个类型为FooImpl的对象,并使用一个名为foo的变量来存储对该对象的引用。 In order for a variable to hold a reference to an object, the variable's type should be compliant to this object. 为了使变量能够保存对对象的引用,变量的类型应与该对象兼容。 Saying compliant, I mean that either the variable's type would be the same as the object you create or would be a base type or an interface. 说兼容,是指变量的类型将与您创建的对象相同,或者是基本类型或接口。 That being said there isn't any load of any interface. 话虽如此,没有任何接口的负担。

This line of code: 这行代码:

IFoo foo = new FooImpl();

says that the foo will hold a reference to an object that implements the IFoo interface, just this. 表示foo将保存对实现IFoo接口的对象的引用,仅此IFoo

Also if there is a another type which inherits from FooImpl (eg FooImpl2) Will this code start from FooImpl2 and walk up the inheritance hierarchy to load IFoo interface? 另外,如果还有另一个从FooImpl继承的类型(例如FooImpl2),此代码是否将从FooImpl2开始并向上继承层次结构以加载IFoo接口?

No 没有

Also, will the IFoo's MethodTable contain a pointer directly to the FooImpl2's method table or will it redirected via the intermediate implementing type (ie FooImpl). 另外,IFoo的MethodTable将直接包含指向FooImpl2的方法表的指针,还是通过中间实现类型(即FooImpl)重定向。

The reference that will be stored in the variable would point to the FooImpl2's method table direclty. 将存储在变量中的引用将指向FooImpl2的方法表直接。 Actually, it would point to the concrete object stored in heap and there would be the reference to the method's table of FooImpl2 . 实际上,它将指向存储在堆中的具体对象,并且将引用FooImpl2的方法表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM