Emit learning-Q & A-difference between call and callvirt

Source: Internet
Author: User
Tags emit

Before Emit In the course of learning, I encountered multiple call methods and found that sometimes I used Call And sometimes use Callvirt I have never understood the differences between the two. Then I checked Msdn , Msdn The two are interpreted:


Call: Call the method indicated by the passed method specifier;

L
callvirt "Times New Roman" ">: call the post-binding method for the object and push the return value to the computing stack.

"Times New Roman"> but I still don't understand it after reading it. I think it may be because of the Chinese version. This afternoon, I saw callvirt command explanation, "binding methods to objects in the later stage" suddenly occurred. Does this seem to mean polymorphism? See virt MSO-Hansi-font-family: "Times New Roman" ">, it should be virtual "Times New Roman"> abbreviation, so I was more certain about my own ideas (I am assigned to ABC and cannot access the Internet. Otherwise, I will find the result in the garden. I'm so sad !), Start practice immediately.

We use the most classicAnimalTo verify this idea, first define the relevant types, as follows:

Animal
Class Animal
{
Public   Virtual   Void Speak ()
{
Console. writeline ( " Animal. Speak " );
}
}

class CAT: animal
{< br> Public override void speak ()
{< br> console. writeline ( " cat. speak " );
}< BR >}

ClassDog: Animal
{
Public Override VoidSpeak ()
{
Console. writeline ("Dog. Speak");
}
}

"Times New Roman" "> because only simple method calls are implemented, so here we choose to use dynamicmethod "Times New Roman" "> instead of creating dynamic Program set, by the way, you can also perform dynamicmethod "Times New Roman">. To use dynamicmethod "Times New Roman" "> first, we need to define a delegate to call methods, definition:

color: Blue; MSO-font-kerning: 0pt; MSO-no-proof: yes "> private MSO-font-kerning: 0pt; MSO-no-proof: yes "> DeleGate
void speakdelegate ( animal animal);

Then we pass inAnimalClass or its derived class instance, and callSpeakMethods To verify the previous ideas. The implementation of the method is relatively simple.CodeThe Code is as follows:

Dynamicmethod
Class Program
{
Private   Delegate   Void Speakdelegate (animal );

Static   Void Main ( String [] ARGs)
{
// Define a dynamic method with no return value. The input parameter is animal, and the module where the input parameter is located is the module where the program class is located.
Dynamicmethod dynamicspeakwithcall =   New Dynamicmethod ( " Dynamicspeakwithcall " , Null , New Type [] { Typeof (Animal )}, Typeof (Program). Module );

Ilgenerator callil=Dynamicspeakwithcall. getilgenerator ();

// the loading parameter 0 is the object of the animal class or its derived class
callil. emit (Opcodes. ldarg_0);
// call the Speak method using the Call Command
callil. emit (Opcodes. call, typeof (animal ). getmethod ( " speak " ));
callil. emit (Opcodes. RET);

Console. writeline ( " Speakwithcall: " );
Speakdelegate speakwithcall = (Speakdelegate) dynamicspeakwithcall. createdelegate ( Typeof (Speakdelegate ));
Speakwithcall ( New Animal ());
Speakwithcall ( New CAT ());
Speakwithcall ( New Dog ());

// Define a dynamic method with no return value. The input parameter is animal, and the module where the input parameter is located is the module where the program class is located.
Dynamicmethod dynamicspeakwithcallvirt =   New Dynamicmethod ( " Dynamicspeakwithcallvirt " , Null , New Type [] { Typeof (Animal )}, Typeof (Program). Module );

Ilgenerator callvirtil=Dynamicspeakwithcallvirt. getilgenerator ();

// the loading parameter 0 is the object of the animal class or its derived class
callvirtil. emit (Opcodes. ldarg_0);
// call the Speak method using the callvirt command
callvirtil. emit (Opcodes. callvirt, typeof (animal ). getmethod ( " speak " ));
callvirtil. emit (Opcodes. RET);

Console. writeline ( " Speakwithcallvirt: " );
Speakdelegate speakwithcallvirt = (Speakdelegate) dynamicspeakwithcallvirt. createdelegate ( Typeof (Speakdelegate ));
Speakwithcallvirt ( New Animal ());
Speakwithcallvirt ( New CAT ());
Speakwithcallvirt ( New Dog ());
}
}

Finally, the output result is shown as follows:

Speakwithcall:

Animal. Speak

Animal. Speak

Animal. Speak

Speakwithcallvirt:

Animal. Speak

Cat. Speak

Dog. Speak

"Times New Roman"> obviously, the previous inference is correct. Download The Source Code call MSO-Hansi-font-family: "Times New Roman" "> and Cal Lvirt MSO-Hansi-font-family: "Times New Roman" "> Differences
MSO-Hansi-font-family: "Times New Roman" ">.

ps MSO-Hansi-font-family: "Times New Roman" ">: learning emit "Times New Roman" "> only a few days ago, so the above analysis seems a little superficial, just simple Record your learning process. If you can give me some in-depth analysis, I am not very grateful.

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.