How does the callvirt .NET instruction work for interfaces?

前端 未结 2 2011
半阙折子戏
半阙折子戏 2021-01-31 08:29

Explaining virtual dispatching to someone is easy: every object has a pointer to a table as part of its data. There are N virtual methods on the class. Every call to a particula

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-31 09:00

    Because the compiler always has to have an actual object on which to call the method (at runtime), it always knows at runtime the concrete type that it is dealing with.

    The code that calls a virtual method firstly determines the type of the object being used. It then consults the type's method table to lookup the method being called. The code then simply calls that method, passing the object's reference as 'this' along with any other parameters.

    I suspect the crucial bit you're interested in is how the code looks up the address of the method in the type's method table.

    More details about the method table are given in the "JIT and Run" article in the May 2005 edition of MSDN magazine (which at the time of writing can be downloaded as a ".chm" from this page - but you will have to go to the file's properties to unlock it before it will display properly, due to security restrictions.)

    It's still a bit hand-wavy about exactly how the lookup is done, but it does give quite a lot of other details.

提交回复
热议问题