Welcome

首页 / 软件开发 / .NET编程技术 / [你必须知道的.NET]第二十六回:认识元数据和IL(下)

[你必须知道的.NET]第二十六回:认识元数据和IL(下)2011-03-23 博客园 Anytao书接上回:

第二十四回:认识元数据和IL(上) , 第二十五回:认识元数据和IL(中)

我们继续。

终于到了,说说元数据和IL在JIT编译时的角色了,虽然两个回合的铺垫未免铺张,但是却丝毫不为过,因为只有充分的认知才有足够的体会,技术也是如此。那么,我们就开始沿着方法调用的轨迹,追随元数据和IL在那个神秘瞬间所贡献的力量吧

5 元数据和IL在JIT编译时

CLR最终执行的只有本地机器码,所以JIT编译的作用是在运行时将IL代码解析为机器码执行。对于JIT编译,我们会以专门的篇幅来全面了解,本文只将目光关注于元数据和IL在程序执行时的作用和参与细节。首先,IL是基于栈执行的,执行方法调用时,方法参数、局部变量还有返回值等被分配于栈上,并执行其调用过程,既然是关注JIT编译时,因此我们自然而然将关注方法的执行,因为JIT编译是以执行方法调用而触发的。

首先,对本文开始的代码加点新料:

// Release : code04, 2009/02/24
// Author : Anytao, http://www.anytao.com
// List : Base.cs
public class Base
{
public void M()
{
Console.WriteLine("M in Base");
}

public virtual void N()
{
Console.WriteLine("N in Base");
}
}

还有:

// Release : code05, 2009/02/24
// Author : Anytao, http://www.anytao.com
// List : Three.cs
public class Three : Base
{
private static int ID { get; set; }

public override void N()
{
//Something new in Three
Console.WriteLine("N in Three");
}

public void M()
{
Console.WriteLine("M in Three");

M1();
}

public void M1()
{
Console.WriteLine("M1 in Three");
}
}

还有执行代码:

static void Main(string[] args)
{
Base three = new Three();
three.M();
three.N();
}