前一篇我们探索了类型的第一种成员:字段。字段在IL编译时,会生成MdToken和偏移量,因为对于类型来说,一个类型在编译时就已经确定了字段的个数,所以偏移量对于编译器来说是已知的,字段和偏移量分别由元数据表(Field和ClassLayout)来记录。
本篇我们来讨论类型中的另一种成员:方法.在本系列的第一篇探索CLR原理系列(1):类型 中我们说到类型中只有两种成员:字段和方法.字段是用来描述类型的状态,而方法则提供了类型所具备的功能.首先我们来看看方法在IL中是如何描述的.先来定义一个类型.
public
class MethodClass
{
private string name= " xulei ";
private int age= 10;
public static int Add(int a,ref int b) // 静态
{
return a + b;
}
private static int Sub(out int a,out int b) // 私有静态
{
a = 10;
b = 5;
return a - b;
}
public void WriteName() // 公有
{
Console.Write(name);
}
protected int GetAge() // 受保护
{
private string name= " xulei ";
private int age= 10;
public static int Add(int a,ref int b) // 静态
{
return a + b;
}
private static int Sub(out int a,out int b) // 私有静态
{
a = 10;
b = 5;
return a - b;
}
public void WriteName() // 公有
{
Console.Write(name);
}
protected int GetAge() // 受保护