[CLR-SOS debugging] Summary of the problem of method table inheritance call

Source: Internet
Author: User

What does the CLR call method mechanism look like? What is the composition of the subclass method table? This article summarizes my practical experiences and provides answers to the questions.

First, the problem is introduced as follows:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace method table composition CLR call Test
{
Public class father
{
Public void dowork ()
{
Console. writeline ("father. dowork ()");
}
Public Virtual void dovirtualwork ()
{
Console. writeline ("father. dovirtualwork ()");
}
Public Virtual void dovirtualwork2 ()
{
Console. writeline ("father. dovirtualwork2 ()");
}
}
Public class son: Father
{
Public new void dowork ()
{
Console. writeline ("son. dowork ()");
}
Public override void dovirtualwork ()
{
Console. writeline ("son. dovirtualwork ()");
}
Public new void dovirtualwork2 ()
{
Console. writeline ("son. dovirtualwork2 ()");
}
}
Class Program
{
Static void main (string [] ARGs)
{
// What is the output below?
Father son2 = new son ();
Son2.dowork ();
Son2.dovirtualwork ();
Son2.dovirtualwork2 ();
}
}
}

What is output? If you are a Daniel and have a clear understanding of this problem, you do not have to read it down. Please stop here to avoid wasting your precious time. I will be ashamed of it. --. If you do not know anything about CLR, Il, and SOS debugging, you can take some time to watch other things ,--!. Boyou VOICE: a lot of nonsense, hurry into the topic! Debuglzq: Now!

Let's take a look at the output of the problem:

Success, this result !!!!!

Maybe you think debuglzq's article is totally unreasonable. What are you doing? I would like to remind you that the topics to be discussed here are indispensable for us to have a deep understanding of the three pillars of the target objects. At least we will use them....

Previously, I had some research on CLR. The correct answer to the question is directly given below! (Debuglzq's incorrect understanding will not be shared with you, so as not to mislead you and be counterproductive .)

Start with the CLR call mechanism:

First, each type (father and son) has its own method table. When the CLR calls a method, it knows whether the method is a virtual method. If it is not a virtual method, check the method table of the variable type. Therefore, son2.dowork () searches for the father MT (if it cannot be found, it will go to the base class ), if it is found, execute it (or run JIT first ). If it is a virtual method, the object on the stack will be found based on the reference, and the real type of the object will be found based on the object type pointer (that is, the return type of the GetType method), so son2.dovirtualwork () the MT of son will be searched directly (if not, the base class will be removed, which is the same as that of non-virtual methods ).

Subclass method table composition: contains the virtual method of the parent class, and does not contain the instance method of the parent class.

For SOS debugging: Summary of msdn recommended for Debug: http://msdn.microsoft.com/zh-cn/library/bb316764.aspx.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.