1

我试图了解存储在方法表中的信息。这是我的代码。

class MyClass
{
    private int x = 60;
    private int y = 90;

    public void MethodB() 
    {
        Console.WriteLine("MethodB");
    }

    public void MethodC()
    {
        Console.WriteLine("MethodC");
    }

    public void MethodA()
    {
        GetHashCode();


        Monitor.Enter(this);

        Console.WriteLine("Attach debugger now");
        Console.ReadKey();
    }


    static void Main(string[] args)
    {
        MyClass mc = new MyClass();
        mc.MethodA();
    }
}

这是对象在内存中的样子

0:000> !do 0x02368a1c   
Name: ConsoleApplication1.MyClass  
MethodTable: 001f3310  
EEClass: 001f136c  
Size: 16(0x10) bytes  
 (C:\Download\PresentationPrep\TechDaysDemos\SomeTesting\bin\Debug\SomeTesting.exe)  
Fields:  
      MT    Field   Offset                 Type VT     Attr    Value Name  
6d032da0  4000001        4         System.Int32  1 instance       60 x  
6d032da0  4000002        8         System.Int32  1 instance       90 y  

然后我转储方法表

0:000> dd 001f3310  
001f3310  00000000 00000010 00050011 00000004  
001f3320  6d030770 001f2f2c 001f334c 001f136c  
001f3330  00000000 00000000 6cf86ab0 6cf86ad0  
001f3340  6cf86b40 6cff7540 008500d0 00000080  
001f3350  00000000 00000000 00000000 00000000  
001f3360  00000000 00000000 00000000 00000000  
001f3370  00000000 00000000 00000000 00000000  
001f3380  00000000 00000000 00000000 00000000  

这是我发现有点混乱的地方。

  1. 第一个字段指示对象的类型(如果它是类或数组等)。我的理解是,对于类,此字段显示0x00040000,而此处仅显示0x00000000.

  2. 第二个字段是对象的大小。这个还行。

  3. 第三场有什么意义00050011

  4. 这个表示继承方法的数量,它指向父对象类的方法ToString、、Equals和。这个对吗?GetHashCodeFinalize

我不了解其他领域,所以如果有人也解释一下,将不胜感激。

4

2 回答 2

1

This is basically an implementation detail and trying to figure out how the CLR works by poking around the internal structures is not easy to say the least. A lot of the internal structures are optimized in various ways, which makes dumping the relevant information hard. I have a similar question here.

If you haven't already looked at it, I recommend you read Shared Source CLI essentials. While it doesn't cover all the gory details, it does give a pretty good explanation of how the shared source CLI is organized.

In my experience some of these structures cannot easily be mapped without the use of utility methods that encapsulates what the CLR is doing internally. That is basically what SOS does for us. If you get the source for the SSCLI, you can dig into the source for the shared source version of SOS for additional details.

Of course the SSCLI isn't the same as the current Microsoft CLR, but in my experience they have a lot in common, so it is usually a good source of information.

There's a detailed write up of how the method table was implemented in .NET 1.x here. It has a lot of details, but alas the implementation has been changed so it isn't valid for the current CLR.

于 2010-11-05T07:56:18.197 回答
1

只需使用

!sos.dumpmt 001f3310

这将为您提供方法表转储,您不必担心会在版本和 SP 之间更改的内部内存布局

于 2010-11-20T19:56:17.637 回答