Understanding IL code (3) and understanding il code

Source: Internet
Author: User
Tags mscorlib

Understanding IL code (3) and understanding il code

Because I want to write my graduation thesis, I have no time to write it recently. I always need to spare time. Ah, this kind of life is annoying.

This article mainly describes the IL code of the delegate, class, and method.

Delegate: it should be clear that the delegate is actually a class. The compiler will compile it into a class that inherits from the MulticastDelegate class. There are three methods in it: BeginInvoke, EndInvoke, and Invoke. When we use the delegate method name to call the method, the compiler actually calls the Invoke method (syntactic sugar ).

The following uses simple code to conceal the IL code of the delegate:

Let's take a look at how the compiler compiles the delegate:

Well, the following is the important IL code. In fact, you can try to parse it yourself. I think it will be better to remember when you try something.

I think most of the commands have been mentioned in the previous two articles, but I will explain them one by one.

. Method private hidebysig static voidMain(String [] args) cilmanaged {. entrypoint// Portal. This is said many times.. Maxstack 2// The maximum number of data items that the evaluation Stack may contain.. Locals init ([0] class TestDemo4.Program/MyDeleDele)// As mentioned above, the delegate is eventually compiled into a class, so here is a class type variable dele, which is stored in the call stack.L_0000: nop // No Operation. L_0001: ldnull// Push the null reference to the computing stack.L_0002: ldftn void TestDemo4.Program/UserInfo: PrintName (string)// Push the native int pointer pointing to the native code that implements the specific method to the computing stack, that is, push the method pointer to the evaluation stack.L_0008: newobj instance void TestDemo4.Program/MyDele:. ctor (object, native int)// Create a delegated instance and press it into the evaluation stack. This step calls the Delegate constructor (. ctor), this constructor requires two parameters, one is the object reference, here is L_0001: ldnull: null object, the second parameter is the action in Rochelle 0002 of the method address.L_000d: stloc.0// Save the delegate instance in the evaluation stack to The 0th position of the call stack.L_000e: ldloc.0// Obtain the value (delegated instance) at The 0th position in the call stack and press it into the evaluation stack.L_000f: ldstr "Helius"// Load the string, create a Helius object in the managed heap, and store the reference on the evaluation stack.L_0014: callvirt instance void TestDemo4.Program/MyDele: Invoke (string)// Invoke. No, the delegate instance calls the Invoke Method for execution. Callvirt can only call instance methods and virtual methods, but cannot call static methodsL_0019: nop// No OperationL_001a: call valuetype [mscorlib] System. ConsoleKeyInfo [mscorlib] System. Console: ReadKey ()// Call the ReadKey method.L_001f: pop// Clear the evaluation stack.L_0020: ret// Return.}

-------------------------------------------------------------------------------- Split line ------------------------------------------------------------------------------------------------

General Code for classes and methods:

Here, we start to parse the Person class. Here I use the automatic attributes, which should be clear to everyone. The Compiler compiles the automatic attributes into a private field and two methods.

Okay. Now we can see the time for the Main function's IL code.

In the above IL code, only one command has not appeared before, that is, the newobj command, which means to create an instance object, and push the Instance Object Reference to the computing stack, that is, the evaluation stack.

I don't want to parse a sentence. Now I will explain the process of instantiating a class in the form of drawing:

In fact, I am only talking about it in general. I think it is absolutely necessary for everyone to write a small Demo by themselves. After compilation, use Reflector to check the IL code, which will be clearer. In addition, I don't know anything about it. I think you can use Baidu or take it out for discussion.

In the next chapter, let's talk about the IL code of process control. I think this part can be completed.

 

 

Related Article

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.