How to get TypeDef from TypeSpec

2019-08-02 02:00发布

I'm trying to implement managed debugger looking at MDBG sample. Currently I'm stuck trying to get base class hierarchy methods using IMetaDataImport.

EnumMethods, that I'm using, enumerates MethodDef tokens representing methods of the specified type. But I want to enumerate all the methods in class hierarchy. To do that I'm using GetTypeDefProps, that returns ptkExtends, which is token representing the base class. The problem is that base class could be represented by TypeDef, TypeRef or TypeSpec.

How can I get base class TypeDef from relative TypeSpec?

I've read ECMA part II specifications, but it didn't help me a lot...

Here is what I got so far:

      int size;                    
      TypeAttributes pdwTypeDefFlags;
      m_importer.GetTypeDefProps(m_typeToken,
                    null,
                    0,
                    out size,
                    out pdwTypeDefFlags,
                    out ptkExtends
                    );

      //ptkExtends is correct TypeSpec token
      IntPtr ppvSig;
      uint pcbSig;
      m_importer.GetTypeSpecFromToken(ptkExtends, out ppvSig, out pcbSig);
      //I'm getting the TypeSpec Blob signature in ppvSig, how to use it to get TypeDef?!

1条回答
Root(大扎)
2楼-- · 2019-08-02 02:56

As stated previously, the TypeSpec format is defined in Partition II Section 23.2.14, its expressed with something resembling EBNF with the terminals defined in section 23.1.16.

A TypeSpec can represent a range of different kinds of types, but the only one that makes sense for a base class is GENERICINST (a closed generic type).

TypeSpecBlob ::= GENERICINST (CLASS | VALUETYPE) TypeDefOrRefEncoded GenArgCount Type Type*
             | ...

TypeDefOrRefEncoded is defined in section 23.2.8, compressed integers are defined at the start of section 23.2 and Type is defined in section 23.2.12.

Type ::= CLASS TypeDefOrRefEncoded
     | VALUETYPE TypeDefOrRefEncoded
     | ...

Given the bytes from your previous example (15 12 3C 01 12 36), my 'back of the napkin' scratching's come up with the following:

15 // GENERICINST
12 //   CLASS
3C //     TypeDefOrRefEncoded = 0200000F (The TypeDef of the open generic type.)
01 //   GenArgCount = 1
12 //   CLASS
36 //     TypeDefOrRefEncoded = 0100000D (The TypeRef of the single type argument.)
查看更多
登录 后发表回答