2016-07-26 41 views
1

我试图实现托管调试器查看MDBG示例。使用IMetaDataImport获取基类层次结构方法EnumMethods

MDBG能够在给定范围内解析函数名称,但它没有考虑基类。

MDBG是这样做的:

/// <summary> 
    /// Resolves a Function from a Module, Class Name, and Function Name. 
    /// </summary> 
    /// <param name="mdbgModule">The Module that has the Function.</param> 
    /// <param name="className">The name of the Class that has the Function.</param> 
    /// <param name="functionName">The name of the Function.</param> 
    /// <returns>The MDbgFunction that matches the given parameters.</returns> 
    public MDbgFunction ResolveFunctionName(MDbgModule mdbgModule, string className, string functionName) { 
     ... 
     foreach (MethodInfo mi in t.GetMethods()) { 
      if (mi.Name.Equals(functionName)) { 
       func = mdbgModule.GetFunction((mi as MetadataMethodInfo).MetadataToken); 
       break; 
      } 
     } 
     return func; 
    } 

虽然Type.GetMethods()被重写,有这个实现,使用IMetaDataImport.EnumMethods:

 public override MethodInfo[] GetMethods(BindingFlags bindingAttr) { 
     ArrayList al = new ArrayList(); 
     IntPtr hEnum = new IntPtr(); 

     int methodToken; 
     try { 
      while (true) { 
       int size; 
       m_importer.EnumMethods(ref hEnum, (int) m_typeToken, out methodToken, 1, out size); 
       if (size == 0) { 
        break; 
       } 
       al.Add(new MetadataMethodInfo(m_importer, methodToken)); 
      } 
     } 
     finally { 
      m_importer.CloseEnum(hEnum); 
     } 
     return (MethodInfo[]) al.ToArray(typeof (MethodInfo)); 
    } 

问题是m_importer.EnumMethods()枚举MethodDef令牌表示指定类型的方法,但我对类层次结构中的所有方法感兴趣。

如何获取类层次结构中定义的所有方法? (显然,由于我在分析其他进程中定义的类型,因此无法使用常见的反射方法)

我对interop和深层CLR/CIL结构的了解有限,因此无法找到正确的方法。

欢迎任何建议/建议!

问候,

回答

2

GetTypeProps将返回ptkExtends基本类型的元数据标记,你可以用它来从每向上走继承树和收集方法,当您去。

但请注意,元数据令牌可能不是TypeDef。它可以是一个TypeRef(需要你解析类型)或TypeSpec(要求你解析类型签名并提取一个合适的TypeDef/TypeRef)。

+0

谢谢!现在我明白我应该做的更好一点,但还不完全清楚。我可以看到MDBG如何使用GetTypeRefProps/GetTypeDefProps方法处理TypeRef和TypeDef。但是我应该如何解析TypeSpec签名? – 3615

+0

使用[GetTypeSpecFromToken](https://msdn.microsoft.com/en-us/library/windows/desktop/hh870637(v = vs.85).aspx)获取签名blob。签署的格式在[ECMA-335](http://www.ecma-international.org/publications/standards/Emama-335.htm)分区II第23.2.14节中定义。 –

+0

再次感谢您!我需要一些时间来尝试一下,然后我会得到一些结果.. – 3615