Judgment Rules for calling methods under. Net inheritance and Polymorphism

Source: Internet
Author: User

Recursively returning from Child class to parent class

If the method is declared on the interface, this method is returned.

If the method is declared on the parent class

If the given method is override, the virtual method (the method override) is returned)

If the given method is new (note that the default method is new), the method itself will be returned (newslot can be seen in IL)

If the method is not defined in the current type, return to start analyzing the parent class of the current class

The following is the original text. I have modified some of the translation content.

If the method is declared on an interface, returns the method.

If the method is defined in a base class, then works as follows:

If a given method overrides a virtual definition in the base class, the virtual definition is returned.

If a given method is specified with the New Keyword (as in newslot as described in common type system), the given method is returned.

If the method is not defined in the type of the object on which getbasedefinition is called, The method definition highest in the class hierarchy is returned.

You can use ilspy to view the generated Il to prove this.

Let's give a detailed example.

1. Subclass does not inherit from parent class

 Namespace  Csharptester
{
Class Program
{
Static Void Main ( String [] ARGs)
{
A
= New A ();
B
= New A ();
A. A1 ();
B. A1 ();
}
}
Public Class A: B
{
}
Public Class B
{
Public Void A1 () {console. writeline ( 1 );}
}
}

The result is that the subclass in the Il command calls B. A1.

2. Implementation of Child class override parent class

 Namespace  Csharptester
{
Class Program
{
Static Void Main ( String [] ARGs)
{
A
= New A ();
B
= New A ();
A. A1 ();
B. A1 ();
}
}
Public Class A: B
{
Public New Void A1 ()
{
Console. writeline (
2 );
}
}
Public Class B
{
Public Virtual Void A1 ()
{
Console. writeline (
1 );
}
}
}

The result is that the subclass in the Il command calls a. A1.

3. Subclass override parent class Method

 Namespace  Csharptester
{
Class Program
{
Static Void Main ( String [] ARGs)
{
A
= New A ();
B
= New A ();
A. A1 ();
B. A1 ();
}
}
Public Class A: B
{
Public Override Void A1 ()
{
Console. writeline (
2 );
}
}
Public Class B
{
Public Virtual Void A1 ()
{
Console. writeline (
1 );
}
}
}

The result is that the subclass in Il calls B. A1.

4. Subclass directly or indirectly inherits the interface (Note whether to call the method or start from the interface; if it is from the type, it is consistent with the above three cases)

 Namespace  Csharptester
{
Class Program
{
Static Void Main ( String [] ARGs)
{
IB
= New A ();
IB B
= New A ();
A A1
= New A ();
B B1
= New A ();

A. A1 ();
B. A1 ();
A1.a1 ();
B1.a1 ();

}
}
Public Class A: B
{
Public New Void A1 ()
{
Console. writeline (
2 );
}
}
Public Class B: IB
{
Public Virtual Void A1 ()
{
Console. writeline (
1 );
}
}
Public Interface IB
{
Void A1 ();
}
}

If the method is called from the interface, IB. A1 is called in Il (no matter what its actual type is). If the type is consistent with the 1-3 point

So far, we know the corresponding il commands for different types of declarations.

When there is no override but new, we can accurately determine the called method.

In the case of override (note that only the virtual method can be override)

The IL Command executed by the sub-class is B. A1 (). How does the CLR make the actually executed method A. A1 () instead of B. A1 ()?

This is because the CLR passes in a parameter when executing B. A1 (), that is, the Instance Object A (C #CodeA In A. A1 () is the Instance Object of)

Now we have an object, but where is the definition of a method?

In. In net, the slot position after the virtual method inherits is not changed. For example, if the slot position of B. A1 () is 5, the slot position of the subclass method A. A1 () is also 5.

Then the result is displayed. First, according to the Il command B. a1 () gets the relative slot location, and then obtains the type definition of a Based on instance parameter A. Similarly, locate Location 5. that is. a1 (), this is the method actually executed

A. A1 ()-> il command is B. A1 ()-> Get slot location->

-> Get A type definition a-> get a method with the same slot position as

-> Execution Method

By the way:

The IL command callvirt is specifically used to handle this situation. (callvirt binds the check instance object during runtime to know where the real method is)

There is also an il command call that does not check this situation (you can find that all the Il commands generated by the static method are call and do not need instance objects)

To be continued

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.