How to get TypeDef from TypeSpec

怎甘沉沦 提交于 2019-12-14 03:58:03

问题


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:


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.)


来源:https://stackoverflow.com/questions/38615100/how-to-get-typedef-from-typespec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!