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