Read the classic-CLR via C # (Jeffrey Richter) Note _ Run-time parsing type reference

Source: Internet
Author: User
Tags mscorlib

 Public Sealed class program{  publicstaticvoid  Main () {    System.Console.WriteLine (  "Hi");}  }

When the program runs, the CLR loads and initializes it. The CLR reads the CLR header of the Assembly, looking for a methoddeftoken that identifies the application entry method (Main). The CLR then retrieves the MethodDef metadata table, finds the offset of the method's IL code in the file, and compiles the Il Code JIT (just-in-time) to compile the cost (native) code. The code is validated at compile time to ensure type safety. Finally, the local code is executed. The IL code for the main method is shown below. (Run ILDasm.exe can be generated)

. method PublicHidebysigStatic voidMain () cil managed//sig:00{. entrypoint//Method begins at RVA 0x2050//Code size One (0XB). maxstack8il_0000:/*72 | (000001)*/ldstr"Hi"il_0005:/*28 | (0A) 000003*/Pagervoid[mscorlib] System.console::writeline (string) il_00a:/*2A |*/ret}//end of Method Program::main

When this code is JIT-compiled, the CLR examines all references to types and members and loads the assemblies that define them, if they are not already loaded. As you can see, the IL code above has a reference to System.Console.WriteLine. Specifically, the IL call Directive applies Token 0a000003. This token corresponds to the record entry in the MemberRef source data table 3.CLR checks the MemberRef record entry, and discovers that one of its fields refers to a record entry (System.Console type) in a TypeRef table. Based on the TypeRef record entry, the CLR is booted to a AssemblyRef record entry: "mscorlib, version=4.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 ". This way, the CLR knows which assembly it needs. Next, the CLR must locate and load the assembly.

When parsing a referenced type, the CLR may find a type in the following three places.

    • Same file
    • Different files, same assembly
    • Different files, different assemblies

A diagram procedure that binds to a type:

Read the classic-CLR via C # (Jeffrey Richter) Note _ Run-time parsing type reference

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.