Explore the CLR principles series (3): Method metadata and IL (suitable for old birds, new people do not indulge in it)

Source: Internet
Author: User

In the previous article, we explored the first type of member: field. The mdtoken and offset will be generated when the field is compiled in Il, because for the type, the number of fields has been determined by a type during compilation, so the offset is known to the compiler, fields and offsets are recorded by the metadata table (field and classlayout.

In this article, we will discuss another type of member: method. The first article in this seriesExploring CLR principles series (1): TypeAs mentioned inThere are only two types of members: Field and method. fields are used to describe the status of the type, while methods provide the functions of the type. first, let's take a look at how the method is described in Il. first define a type.

 

Public Class Methodclass
{
Private String Name = " Xulei " ;
Private Int age = 10 ;
Public Static int Add (Int A, ref int B)// Static
{
Return A + B;
}
Private Static int Sub (Out int A, out int B) // Private Static
{
A = 10 ;
B = 5 ;
Return A-B;
}
Public Void Writename () // Public
{
Console. Write (name );
}
Protected Int getage () // Protected
{
Return age;
}
Private Void Writenameandage () // Private
{
Console. Write (name + age. tostring ());
}

In this type, we define two static methods with different access levels and three instance methods with different access levels. Let's take a look at the metadata of these methods.

Typedef # 2 ( 02000003 )
-------------------------------------------------------
Typdefname: Testdemo1.type. methodclass ( 02000003 ) // Type ID

Method # 1 (06000003 ) // Method ID
-------------------------------------------------------
Methodname: add (06000003)
Flags :[ P Ublic] [ Static]
RVA: 0x00002064
Returntype: I4
2 arguments // list of parameters
Argument # 1 : I4 // value transfer
Argument # 2 : Byref   I4

// Reference Transmission
2Parameters
(1) Paramtoken :(08000001) Name:Flags:[None]
(2) Paramtoken :(08000002) Name: BFlags:[None]

Method #2 ( 06000004 )
-------------------------------------------------------
Methodname: Sub ( 06000004 )
Flags :[ Private ] [Static]
RVA:0x0000207c
Returntype: I4

The yellow part is the method signature.

2 Arguments
Argument # 1 : Byref I4
Argument # 2 : Byref I4
2 Parameters is the content of the parameter metadata table.
( 1 ) Paramtoken :( 08000003 ) Name: Flags: [ Out ]
( 2 ) Paramtoken :( 08000004 ) Name: B Flags: [ Out ]

Method # 3 (06000005 )
-------------------------------------------------------
Methodname: Writename ( 06000005 )
Flags: [ Public ]
RVA:Zero X 00002091
Hasthis method Signature
Returntype: Void
No arguments.

Method # 4 ( 06000006 )
-------------------------------------------------------
Methodname: Getage ( 06000006 )
Flags: [Family]
RVA: 0x000020a0
Hasthis method Signature
Returntype: I4
No arguments.

Method # 5 ( 06000007 )
-------------------------------------------------------
Methodname: Writenameandage ( 06000007 )
Flags :[ Private ]
RVA: 0x000020b8
Hasthis method Signature
Returntype:   Void
No arguments.

 We can see that the original data of the method contains a complete description of the method,CodeThe highlighted part contains two parts: the method return value, and the method parameters. These two parts are called method signatures. What are the differences between static methods and instance methods? Let's take a closer look at hasthis. We will discuss the cause later. as we mentioned in the previous article, fields in the DLL have a field metadata table to describe all fields, so the method also has a method metadata table. let's see what it looks like.

 

 The token of the metadata table of the method starts with 0x06, And the token is the ID data in Il. For example, the ID of the Static Method Add is 06000003, and the visibility, abstraction, virtual, and static are marked in flags, mark of sealing and other methods. signature is the method signature, which consists of the return value and parameters. RVA is the relative address of the method body in the DLL file, that is, the relative address of the method code.

In addition, there is also the starting position of the method parameter. in the type section, we have discussed the starting position of the field and the starting position of the method. The starting position of the parameter here is the same as that of the preceding two parameters, the parameter used to find the method. let's take a look at the metadata table of the method parameters.

 

 In the above il code, the parameters of the add method are passed as one value and one reference is passed, so they are marked as byref in Il and flags of the out parameter are marked as out, the ref parameter is no different from the flags parameter of the value passing parameter, and all of them are none. How can we determine the CLR at runtime? The key lies in the Il code. We can see a de-address operator similar to C ++ &.

. Method Public Hidebysig Static Int32 Add (Int32 A,
Int32 & B)

 This is the difference between the transfer of reference and value, and the out parameter is the same, but the flags in the original data table of the parameter mark an out. So what does the other flag opt of flags in the original data table mean ?? You can try to define a variable parameter.Params intA. Check it out.

What does sequence mean? It represents the location of the parameter, that is, the number of parameters. Therefore, the sequence value cannot be greater than the number of parameters of the method. 

 Maybe you will ask, how is the parameter type not in the parameter metadata table? This secret is in the method signature... Do you understand?

 

Summary

We understand that the method is composed of metadata and IL code. Metadata includes the method signature, method parameters, and parameter descriptions. The IL Code contains the method execution logic. Each method has a record in the method metadata table. This record includes the identifier token of the method during compilation, the visibility of the method, and the flags of the Contract features, there is also a signature composed of the parameters and return values of the method, and even the features of each parameter of the method are referenced, which is recorded by the parameter metadata table.

 

 

Next, let's talk about the situation of methods in type inheritance...

 

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.