How and when does .NET actually compile code?

时光毁灭记忆、已成空白 提交于 2019-11-28 20:55:15
Jorge Córdoba

When you compile in VS

  1. Your source code is compiled into a byte code known as the common intermediate language (CIL) or MSIL (Microsoft Intermediate Language).
  2. Metadata from every class and every methods (and every other thing :O) is included in the PE header of the resulting executable (be it a dll or an exe).
  3. If you're producing an executable the PE Header also includes a conventional bootstrapper which is in charge of loading the CLR (Common language runtime) when you execute you executable.

When you execute:

  1. The bootstraper initializes the CLR (mainly by loading the mscorlib assembly) and instructs it to execute your assembly.
  2. The CLR executes your main entry.
  3. Now, classes have a vector table which hold the addresses of the method functions, so that when you call MyMethod, this table is searched and then a corresponding call to the address is made. Upon start ALL entries for all tables have the address of the JIT compiler.
  4. When a call to one of such method is made, the JIT is invoked instead of the actual method and takes control. The JIT then compiles the CIL code into actual assembly code for the appropiate architecture.
  5. Once the code is compiled the JIT goes into the method vector table and replaces the address with the one of the compiled code, so that every subsequent call no longer invokes the JIT.
  6. Finally, the JIT handles the execution to the compiled code.
  7. If you call another method which haven't yet being compiled then go back to 4... and so on...

I post the answer here too as the other question was not really about this...

It is compiled down to IL at, well, compile time. Reflector's magic is that it "understands" the IL and converts it back into c# (or VB.NET or whatever. Look under the Options menu in Reflector and you can view the assembly in any format, including the IL).

In Reflector, you actually are not seeing your original code. You are seeing a translation of the IL into c#. Most of the time that will be very similar to what you wrote, but there are some telltale signs - for example, find a place where you implemented an auto-property:

string MyProperty {get;set;}

And you'll see what that actually compiles to, which is something like this:

public string MyProperty
{
    [CompilerGenerated]
    get
    {
        return this.<MyProperty>k__BackingField;
    }
    [CompilerGenerated]
    set
    {
        this.<MyProperty>k__BackingField = value;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!