How is a method resolved when using callvirt in the CLR?
Is it done at compile time? Because the index into the MethodTable of the method to call could be determined at compile time.
Or ist it done at runtime?
And w开发者_高级运维hat is the method token argument to callvirt?
I've done a blog post about how callvirt works at runtime.
At compile time, the argument to the callvirt
instruction takes a concrete MethodDef
or MemberRef
token, referring to the 'base' method to call (this defines the method arguments, among other things)
At runtime, as part of doing type initialization, the CLR works out which methods override each other, and assigns a slot in the MethodTable
vtable for each virtual 'base' method. Each type then fills that slot with a pointer to whatever actual method implementation should be used for virtual calls to that base method for instances of that type. That is then used to do the virtual call, as described in my post.
When you have virtual method or Instance method and you compiles them. Your method will have callvirt token and will be called by CLR as callvirt, unless static method will have call token and will be called directly.
Callvirt will led the method to be called as virtual at runtime.
At runtime, CLR will examine your method whether its object has been instantiated. And CLR also examine whether your method has been overriden by its derived type. If it is not overriden, your method will be called. If Yes, then its derived's method will be called.
精彩评论