Before JIT
When CLR calls a method for the first time, the JIT code is required first.
Previously, the CLR was required to ensure that all required assemblies can be loaded successfully. If load succeeds, CLR will use metadata in Assembly to create the type itself (type object) to be used on the managed stack)
The structure of the type object, which consists of two parts:
The first part includes the type object pointer, the sync block index (all objects on the managed stack will have these two fields), and the static filed defined by this type.
The second part is the method table defined by the type (including the static method)
Then start JIT
Runtime
After JIT, you can start execution.
First, execute prologue code (almost all methods are available). The local variable is allocated memory on the stack and initialized to null or 0.
Run
When you need to create an object
Create on managed stacks, including type object pointer, sync block index, and instance Field
Next, point the type object pointer to the created type object, initialize the sync block index and the instance field is null or 0, and then call the constructor.
After the preceding steps, the new operator returns the address of the object on the managed stack.
When the static method is called
The JIT compiler finds the method entry in the method table of the type object, and runs the JIT method (if required)
When a non-virtual instance method is called
The JIT compiler tries to find the method table entry of the type object. If the entry cannot be found along the inheritance tree of the object to the parent class, it will execute
When you call a virtual instance method
At this time, the JIT compiler will execute some additional code (this method will be executed every time it is called). First, find the object of the caller on the hosting stack, and its type object pointer locates the caller's type object, and then continues to find the method entry balabala.
Finally, execute the epilogue code (almost all methods are available) and perform some cleanup.
At the beginning of the process
CLR will immediately create a system in the managed heap. type object (whose type Object Pointer Points to itself). All the type object pointers created at runtime point to this system. type type object
In fact, the object. GetType method returns the address stored in the type object pointer.